Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Need help with Regex

by ewhitt (Scribe)
on Nov 13, 2008 at 09:10 UTC ( [id://723378]=note: print w/replies, xml ) Need Help??


in reply to Re: Need help with Regex
in thread Need help with Regex

Interesting. I have never seen "split" used like that before. How could I pass it a variable? Say I wanted to print "four" ?
$output = "one two three four five"; $output = (split $line)[4];
Thanks!

Replies are listed 'Best First'.
Re^3: Need help with Regex
by toolic (Bishop) on Nov 13, 2008 at 14:24 UTC
    The parentheses around split forces it to return an array. The square brackets are used to select a single element from the array returned by split. We could either use a constant numeric value, such as 3, or we could use a scalar variable, such as $col to select the array item. Keep in mind that Perl arrays start at 0, not 1. So, "one" becomes array element [0], and "four" is element [3]:
    use strict; use warnings; my $col = 3; my $str = "one two three four five"; my $output = (split /\s+/, $str)[$col]; print "output=$output\n"; __END__ output=four

Log In?
Username:
Password:

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

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

    No recent polls found