Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

python-like split+array access -- possible?

by nmerriweather (Friar)
on Oct 26, 2005 at 19:37 UTC ( [id://503144]=perlquestion: print w/replies, xml ) Need Help??

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

in python you have:
myString = '123_456' print myString.split('_')[1] --- you get '456'
how can i do something like that in perl?
$myString = '123_456' print ( split '_' , $myString )[1] --- you get '456'
edit: i actually answered myself correctly with my pseudocode description. there was a typo elsewhere in the program that made this not look right. i'm leaving this here for others.

Replies are listed 'Best First'.
Re: python-like split+array access -- possible?
by Juerd (Abbot) on Oct 26, 2005 at 19:50 UTC

    Pay attention to the "if it looks like a function, it IS a function" rule. If a bare identifier is followed by an opening paren, even with whitespace in between, the paren set is taken to be part of the sub call, and whatever follows it, is NOT.

    print (1 + 2) * 3; # prints: 3, not 9 print (split)[1]; # syntax error print ((1 + 2) * 3); # prints 9 print((1 + 2) * 3); # same print +(1 + 2) * 3; # same print ((split)[1]); # works as expected print((split)[1]); # same print +(split)[1]; # same
    The + doesn't *do* anything, but it is not a paren so it breaks the function() parsing.

    In Perl 5, split sees any string given as the pattern as a regex (unless the string is " ", a single normal ASCII space). It is better to write /_/ instead of '_', to make this clear to readers of your code, and to avoid getting hurt when you want / / and write ' ' instead.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


    Perl 6 changes:

    • unary + is no longer a no-op, but provides Num context to its RHS
    • the parens are only seen as function call parens (rather than grouping parens) if the opening paren comes directly after the sub name, with no whitespace in between
    • you can put a dot in between subname and subcall paren, and left of that you are allowed to use whitespace
    • split is directly accessible with .[] or [] (e.g. split("_", $foo)[1])

Re: python-like split+array access -- possible?
by Corion (Patriarch) on Oct 26, 2005 at 19:46 UTC

    If you used the warnings pragma, perl would have told you what is wrong:

    C:\>perl -we "print ( split '_' , $myString )[1]" print (...) interpreted as function at -e line 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

    The following works:

    C:\>perl -we "$myString = '123_456'; print( ( split '_' , $myString )[ +1] )" 456

    The difference is that print is not a keyword in Perl but a function with optional parentheses, unlike in Python where it is a keyword which doesn't need parentheses.

    As an aside, you shouldn't use strings as the first argument to split but regular expressions, as split will interpret the string as a regular expression as well, but passing a regular expression makes it more explicit what happens:

    C:\>perl -we "$myString = '123_456'; print( ( split /_/ , $myString )[ +1] )" 456
Re: python-like split+array access -- possible?
by neniro (Priest) on Oct 26, 2005 at 19:52 UTC
    Or just use an anonym array:
    my $myString = '123_456'; print [split /_/ , $myString]->[1];

      Or just use an anonym array

      The construction and destruction of this temporary array do have some overhead that may be unwanted because of permormance needs.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found