Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: variable mystery

by jeah (Novice)
on Nov 29, 2008 at 05:27 UTC ( [id://726731]=note: print w/replies, xml ) Need Help??


in reply to Re: variable mystery
in thread variable mystery

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 }

Replies are listed 'Best First'.
Re^3: variable mystery
by Anonymous Monk on Nov 29, 2008 at 05:37 UTC
    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 /\|/;

        In oko1's modification, grep is being used for side effects. grep returns the values of the list (in this case split result) for which the expression is true (in this case it is 1 for any and every input). Substitutions as a side effect just happen to be before forcing the test to be true.

        Do modify your code if you are using perl 5.8.7. Either use an explicit loop variable other than $_ ($in should have worked in you had supplied it to split instead of $_; related code is missing)...

        for my $in ( <$fh> ) { my @p = map { ... } split /\|/ , $in; ... }

        ... or just omit it ...

        for ( <$fh> ) { my @p = map { ... } split /\|/ , $_; ... }

        As oko1 said that 1 is the return value of s/// on success; false or empty string on failure. Using a comma, you were generating a 2-element list of s/// return value & $_ (as the last expression of map). Unlike oko1's statement about returning empty $_, returning $_ works just fine ...

        print join ', ' , map { s/^\s+// ; s/\s+$// ; $_ ; } split /\|/ , 'p | q|r|s | t' ;

        In any case, there is no need of a map or a grep with side effects since split can take care of the spaces itself as it takes a regular expression to split on ...

        print join ', ' , split /\s*\|\s*/ , 'p | q|r|s | t' ;

        There are also Text::CSV* modules for your parsing needs.

        Back to your problem ...

        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.
        # input. Baw|Vao|111 Noa St||NewYork|NY|10012|2123456789|123456789
        # code. my @fields = (map { s/^\s+//; s/\s+$//, $_ } split /\|/, $_);

        So, after split, map produced ('', Baw) for Baw, ('', Vao) for Vao and so on. The array then would have an empty string at index 0, Baw at 1, empty string at 2, Vao at 3 and so on, so forth. Since you had used \r, you missed the empty strings.

Re^3: variable mystery
by ikegami (Patriarch) on Nov 29, 2008 at 12:44 UTC

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found