Simple tr/// script

Discussion in 'Perl Misc' started by Mike Flannigan, Aug 14, 2003.

  1. 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.
     
    Mike Flannigan, Aug 14, 2003
    #1
    1. Advertising

  2. Mike Flannigan

    Bob Walton Guest

    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
     
    Bob Walton, Aug 14, 2003
    #2
    1. Advertising

  3. 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
     
    Eric J. Roode, Aug 14, 2003
    #3
  4. 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.
     
    Mike Flannigan, Aug 14, 2003
    #4
  5. Abigail wrote:

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


    That is so cool.
     
    Mike Flannigan, Aug 14, 2003
    #5
  6. 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
     
    Tad McClellan, Aug 14, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. geoffbache
    Replies:
    5
    Views:
    966
    geoffbache
    Jun 13, 2008
  2. Rajat
    Replies:
    3
    Views:
    746
    Jorgen Grahn
    Jan 8, 2010
  3. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    601
    Jan Kaliszewski
    Jan 7, 2010
  4. dillon
    Replies:
    1
    Views:
    189
    higabe
    Nov 17, 2003
  5. Wladimir Borsov
    Replies:
    4
    Views:
    323
    Tintin
    May 11, 2006
Loading...

Share This Page