Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: regex needed

by Chady (Priest)
on Mar 14, 2003 at 14:18 UTC ( #243050=note: print w/replies, xml ) Need Help??


in reply to regex needed

is foo always the second item?
print (split /:/)[1];

Update: I just noticed that this does not actually run, it spits a syntax error, this, however, works:

print '', (split /:/)[1];
Anyone can explain this a bit? got anything to do with list|scalar context?
He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

Replies are listed 'Best First'.
Re:x2 regex needed
by grinder (Bishop) on Mar 14, 2003 at 14:44 UTC

    Operator precedence and print's hunger for a filehandle get in the way. perl parses that as

    (print (split /:/))[1];

    And therefore thinks that (split /:/) should be a filehandle (not sure about that) and then you're taking a slice of the whole mess (which definitely won't fly). Extra parentheses are all you need.

    print( (split /:/)[1] );

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Re: regex needed
by thelenm (Vicar) on Mar 14, 2003 at 17:48 UTC
    Another way to keep the parentheses from being grabbed by print is to add a + in front of them, like this:
    print +(split /:/)[1];
    perlfunc explains why this happens: Perl built-in functions can be called with or without parentheses, so when perl sees parentheses there, it associates them with the function call, not with the creation of a list.

    -- Mike

    --
    just,my${.02}

Re: Re: regex needed
by MrYoya (Monk) on Mar 14, 2003 at 19:52 UTC
    I believe that the
    print (split /:/)[1];
    is not working because it first evaluates the print (split /:/), which returns a scalar value, in this case "1" for a successful print. So it's trying to get a list element of a scalar, which does not work. Same as trying 1[0]; .

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://243050]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-07-21 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (444 votes). Check out past polls.

    Notices?