http://qs1969.pair.com?node_id=726697


in reply to variable mystery

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

Replies are listed 'Best First'.
Re^2: variable mystery
by ikegami (Patriarch) on Nov 29, 2008 at 02:59 UTC

    For one thing, your code doesn't run

    With 5.10:

    >perl -le"for my $_ (1..2) { print }" 1 2
Re^2: variable mystery
by jeah (Novice) on Nov 29, 2008 at 05:27 UTC

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

        Thanks, I guess that's another syntax check I didn't learn.

        Hi again Oko1, your suggested code has a different syntax, especially the addition of the '1' arg, can you tell me what it does? Thanks.

        my @fields = grep { s/^\s+|\s+$//g; 1; } split /\|/;

      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

        Thanks very much for clarifications, everyone. Perl's magical but I need much practice, ugh. :)
Re^2: variable mystery
by CountZero (Bishop) on Nov 29, 2008 at 08:10 UTC
    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";
Re^2: variable mystery
by ikegami (Patriarch) on Nov 29, 2008 at 12:50 UTC

    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
Re^2: variable mystery
by mandarin (Hermit) on Nov 29, 2008 at 12:52 UTC
    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?