Simple tr/// script

M

Mike Flannigan

I can't believe I can't figure this out, but I could use some
help here. I have so many questions about this script,
but I'm going to limit myself to only 3 with this post.

1) The script runs as written, but gives an error after running
"Use of uninitialized value in transliteration (tr///) . . . at line 12,

<DATA> line 3." Then there are more errors about
concatenation at line 13 and 16.
Can anybody explain these errors for me. I can't figure it out.
I thought I was smart, but Perl is proving me wrong.

2) Why won't this script run is I remove the space between the
<DATA> and the line with "Gur ebbgf bs gur snyyra gerr ner
evtug orfvqr gur genvy naq gur gerr unf snyyra gb gur evtug.
Whfg sbyybj gur gerr."? I have only one new line after the
data line. I have tried with and without a new line (carriage
return) at the end.

3) Why won't this script process the whole thing if I put a
newline in the middle like so:
Gur ebbgf bs gur snyyra gerr ner evtug orfvqr
gur genvy naq gur gerr unf snyyra gb gur evtug. Whfg sbyybj gur gerr.


I'm guessing these are somewhat related.
__________________________________

use strict;
use warnings;

my $ki;
my @kites;
while (<DATA>) {
$ki = <DATA>;
push @kites, $ki;
}

foreach my $num (@kites) {
$num =~ tr/a-zA-Z\. /n-za-mN-ZA-M\. /;
print "$num\n";
}

print "$kites[0]\n$kites[1]\n";


__DATA__

Gur ebbgf bs gur snyyra gerr ner evtug orfvqr gur genvy naq gur gerr unf
snyyra gb gur evtug. Whfg sbyybj gur gerr.
 
B

Bob Walton

Mike Flannigan wrote:

....
1) The script runs as written, but gives an error after running
"Use of uninitialized value in transliteration (tr///) . . . at line 12,
<DATA> line 3." Then there are more errors about
concatenation at line 13 and 16.
Can anybody explain these errors for me. I can't figure it out.
I thought I was smart, but Perl is proving me wrong.


The root of your problem is your two statements (shown here with
commentary):

....
while (<DATA>) { #read a line from filehandle DATA to default
#variable $_ and execute a block of code
#if not end of file
$ki = <DATA>; #read a line of code and put it in scalar $ki
....

So you see that you read a line of data, discard it, and read another
line of data which you proceed to process. Then you repeat. So you
process every second line of data. This problem also leads to why it is
doing all the other undesired things you are complaining about.

Recognize that the construct <FILEHANDLE> in scalar context reads a line
from FILEHANDLE, whether it is in a while loop or a scalar assignment,
or anywhere else where a scalar context exists. In a list context, it
will read the whole file in one gulp.

The fix? The second line above should be:

$ki=$_;

2) Why won't this script run is I remove the space between the
<DATA> and the line with "Gur ebbgf bs gur snyyra gerr ner
evtug orfvqr gur genvy naq gur gerr unf snyyra gb gur evtug.
Whfg sbyybj gur gerr."? I have only one new line after the
data line. I have tried with and without a new line (carriage
return) at the end.
3) Why won't this script process the whole thing if I put a
newline in the middle like so:
Gur ebbgf bs gur snyyra gerr ner evtug orfvqr
gur genvy naq gur gerr unf snyyra gb gur evtug. Whfg sbyybj gur gerr.


I'm guessing these are somewhat related.
__________________________________

use strict;
use warnings;

my $ki;
my @kites;
while (<DATA>) {
$ki = <DATA>;
push @kites, $ki;
}

foreach my $num (@kites) {
$num =~ tr/a-zA-Z\. /n-za-mN-ZA-M\. /;
print "$num\n";
}

print "$kites[0]\n$kites[1]\n";


__DATA__

Gur ebbgf bs gur snyyra gerr ner evtug orfvqr gur genvy naq gur gerr unf
snyyra gb gur evtug. Whfg sbyybj gur gerr.
HTH.
 
E

Eric J. Roode

You are calling while(<DATA>) which is reading a line into $_ .
Immediately thereafter, you call $ki = <DATA> which is reading in
ANOTHER line. In effect, you are reading a line of DATA into $_,
ignoring it, then reading the NEXT line into $ki. Try using

while ($ki = <DATA>)
{
push @kites, $ki;
}

instead.

Or simply:

@kites = <DATA>;
 
M

Mike Flannigan

Tad said:
All of the messages that perl might issue are documented in
the perldiag.pod std doc, you should look up messages
there as a 1st debugging step.

Or, ask perl to do the looking-up for you by adding

use diagnostics;

near the top of your program, then execute it again.

^ ^
^ ^ useless backslashes

Why do you want to replace dot with dot and space with space?

Just leave them alone if you do not want them changed.

$num =~ tr/a-zA-Z/n-za-mN-ZA-M/;

Thank you. Very helpful to know that. I guess the warning
was due to the <DATA> read after the file was already at
the end.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top