Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simple tr/// script

Reply
Thread Tools

Simple tr/// script

 
 
Mike Flannigan
Guest
Posts: n/a
 
      08-13-2003

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.


 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      08-14-2003
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.

--
Bob Walton

 
Reply With Quote
 
 
 
 
Eric J. Roode
Guest
Posts: n/a
 
      08-14-2003
Christopher Shatto <> wrote in news:9tA_a.11650
$:

> 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>;

--
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
 
Reply With Quote
 
Mike Flannigan
Guest
Posts: n/a
 
      08-14-2003


Tad McClellan wrote:

> 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.
>
> > $num =~ tr/a-zA-Z\. /n-za-mN-ZA-M\. /;

> ^ ^
> ^ ^ 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.


 
Reply With Quote
 
Mike Flannigan
Guest
Posts: n/a
 
      08-14-2003

Abigail wrote:

> while (<DATA>) {
> tr /a-zA-Z/n-za-mN-ZA-M/;
> print;
> }
>
> Abigail


That is so cool.

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-14-2003
Mike Flannigan <> wrote:
>
> Abigail wrote:
>
>> while (<DATA>) {
>> tr /a-zA-Z/n-za-mN-ZA-M/;
>> print;
>> }
>>
>> Abigail

>
> That is so cool.



Yeah, now you'll be able to read those dirty/offensive jokes.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a script from another script and other script does notdo busy wait. Rajat Python 3 01-08-2010 02:05 PM
RE: How to execute a script from another script and other script doesnotdo busy wait. VYAS ASHISH M-NTB837 Python 2 01-07-2010 08:18 PM
Converting a simple python script to a simple windows executable geoffbache Python 5 06-13-2008 08:51 PM
How to call a simple perl script from HTML without need of HTTPS but simple HTTP ? Wladimir Borsov Perl Misc 4 05-11-2006 09:29 AM
Simple problem with a simple chat script dillon Javascript 1 11-17-2003 11:11 PM



Advertisments