Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

variable mystery

by jeah (Novice)
on Nov 28, 2008 at 23:35 UTC ( [id://726694]=perlquestion: print w/replies, xml ) Need Help??

jeah has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I hope my questions aren't trivial, I can't figure out why replacing the default $_ with an arbitrary variable gives different results, my questions follow, hope you can help, thanks :)

I'd also like to know why if I specify even-numbered elements, nothing returns, eg. if I type

print $fields[0], "\r";

on line 12 in my script, I get nothing. I've found that any even-numbered element specified on line 12 gives me nothing, it's odd, I thought the 0th element would be 'Baw', the 1st element would be 'Vao' and so on. The pipe is discarded by perl, so I don't understand why the even-numbered elements are 'skipped', so to print 'Baw', I type

print $fields[1], "\r"

and to print 'Vao', I type

print $fields[3], "\r"

1. If I use $in on Lines 6 - 8 and print any elements, the entire lines of the file are returned without being processed (with pipes included). I thought I could substitute $_ with any variable. Why won't it work here?

2. If I use $_ on these lines instead, then I get good results (eg. I get the element specified on Line 12.

3. If I UnComment Lines 9 - 11, a '1' is returned for each record/line between the last 2 elements from the file. Why is that?

1 #!/usr/bin/perl -w 2 use strict; 3 4 open my $datfh, '<', 'C:\begperl\my scripts\cdata.txt' or die "Could +n't open file: $!\n"; 5 6 foreach my $_ (<$datfh>) { 7 my @fields = (map { s/^\s+//; s/\s+$//, $_ } # eliminates irr +elevant spaces 8 split /\|/, $_); 9 # for my $i (0 .. $#fields) { 10 # print $fields[$i], "\n" unless $fields[$i] =~ /\r|\n/; 11 # } 12 print $fields[1], "\r"; 13 } 14 close $datfh;

My File's data (for testing):

Baw|Vao|111 Noa St||NewYork|NY|10012|2123456789|123456789

Vca|Wxr|384 Mkl Ln|Xillo|Crrnt Stt|CT|05506|1015567781|1015567782

Uaa|Kvbr|805 Test Rd|Zero|This St|MN|17205|3018757203|3012986736

Caa|Lvbr|905 Test Rd|Bero|That St|MD|12705|3028887203|3028886736

Eaa|Pvbr|311 Zest Rd|Tero|My St|MI|12505|3018757203|3012986736

Replies are listed 'Best First'.
Re: variable mystery
by oko1 (Deacon) on Nov 29, 2008 at 00:40 UTC

    For one thing, your code doesn't run:

    Can't use global $_ in "my" at /tmp/tst line 4, near "my $_ " Execution of /tmp/tst aborted due to compilation errors.

    For another, all you're doing with that "map" statement is returning (via $_) the return of the substitution statement ('1') rather than the modified value of $_ - printing "@fields" would be instructive here.

    Lastly, you're explicitly splitting $_ inside your loop; changing the iterator variable is thus going to leave $_ empty, which means that your code isn't going to work. Here's a slightly simpler version of your code, both with an explicit iterator and without:

    #!/usr/bin/perl -w use strict; for my $foo (<DATA>) { my @fields = grep { s/^\s+|\s+$//g; 1; } split /\|/, $foo; print "$fields[1]\n"; } __END__ Baw|Vao|111 Noa St||NewYork|NY|10012|2123456789|123456789 Vca|Wxr|384 Mkl Ln|Xillo|Crrnt Stt|CT|05506|1015567781|1015567782 Uaa|Kvbr|805 Test Rd|Zero|This St|MN|17205|3018757203|3012986736 Caa|Lvbr|905 Test Rd|Bero|That St|MD|12705|3028887203|3028886736 Eaa|Pvbr|311 Zest Rd|Tero|My St|MI|12505|3018757203|3012986736
    #!/usr/bin/perl -w use strict; for (<DATA>) { my @fields = grep { s/^\s+|\s+$//g; 1; } split /\|/; print "$fields[1]\n"; } __END__ Baw|Vao|111 Noa St||NewYork|NY|10012|2123456789|123456789 Vca|Wxr|384 Mkl Ln|Xillo|Crrnt Stt|CT|05506|1015567781|1015567782 Uaa|Kvbr|805 Test Rd|Zero|This St|MN|17205|3018757203|3012986736 Caa|Lvbr|905 Test Rd|Bero|That St|MD|12705|3028887203|3028886736 Eaa|Pvbr|311 Zest Rd|Tero|My St|MI|12505|3018757203|3012986736

    The output in both cases is:

    Vao Wxr Kvbr Lvbr Pvbr

    Update: I just noticed one more thing: you're using "\r" when you're printing - which means that you're going to overwrite every line that you print. This means that you'll only see the last line, plus anything left over (i.e., anything that was past the current last character) from the previous lines. The correct character to use, at leat in the Unix world, is "\n"; for DOS-based systems, "\n\r" is appropriate.


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf

      For one thing, your code doesn't run

      With 5.10:

      >perl -le"for my $_ (1..2) { print }" 1 2

      Thanks very much Oko1, sorry I'm not too familiar with perl, I've gone through a book on it and I'm just running through some scenarios to boost my fluency. Every bit of advice helps, like the

      "\n\r" tip.

      To: Ikegami & Oko1, I'm not sure why you couldn't run the code, I've perl v5.10.0 I can't find the -le 'switch' Ikegami typed, can you tell me a bit about the following line? I really appreciate your advice :)

      perl -le"for my $_ (1..2) { print }
        C:\>perl -v This is perl, v5.8.7 built for MSWin32-x86-multi-thread ... C:\>perl -MO=Deparse -le"for my $_ (1..2) { print } Can't use global $_ in "my" at -e line 1, near "my $_ " -e had compilation errors. BEGIN { $/ = "\n"; $\ = "\n"; } foreach $_ (1 .. 2) { print $main::_; } C:\strawberry\perl\bin>perl -MO=Deparse -le"for my $_ (1..2) { print } BEGIN { $/ = "\n"; $\ = "\n"; } foreach my $_ (1 .. 2) { print $_; } -e syntax OK C:\strawberry\perl\bin>perl -v This is perl, v5.10.0 built for MSWin32-x86-multi-thread ...

        To: Ikegami & Oko1, I'm not sure why you couldn't run the code

        heh? I showed it does run in 5.10. my $_ is a feature introduced in 5.10.

        I can't find the -le 'switch' Ikegami typed,

        -l sets $\ to "\n".
        -e causes the following value to be treated as a Perl code to execute.
        See perlrun

      The correct character to use, at leat in the Unix world, is "\n"; for DOS-based systems, "\n\r" is appropriate.
      Actually, \n works always and Perl will do "The Right Thing" depending on your OS.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        True as long as there's not networking involved... After all, Perl only knows what system it's being served from, not necessarily what system it's serving to.

        for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

      for DOS-based systems, "\n\r" is appropriate.

      No.
      \n is transformed to \r\n on output.
      \r\n is transformed to \n on input.
      So "\n" is the appropriate choice for "DOS-based" systems.

      >debug file -rcx CX 000A : -d100 la 137B:0100 61 62 63 0D 0A 64 65 66-0D 0A abc..def. +. -q >perl -le"while (<>) { print(unpack('H*', $_)) }" file 6162630a 6465660a
      The correct character to use, at leat in the Unix world, is "\n"; for DOS-based systems, "\n\r" is appropriate.
      Shouldn't this be "\r\n", as the DOS line-end is marked by CR LF?
Re: variable mystery
by parv (Parson) on Nov 29, 2008 at 08:03 UTC
    Could you please not add line numbers before the code meant to be checked by others?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://726694]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 23:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found