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

how to split a string and take certain position?

by adrive (Scribe)
on Sep 24, 2007 at 03:46 UTC ( [id://640647]=perlquestion: print w/replies, xml ) Need Help??

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

i was wondering if it's possible to take a certain position after splitting a string without the use of an array?

something like...

$sentence = "5:83:24"; print split(/:/, $sentence)[1]; #returns 83


if not..is there any way to split and take the remaining string after a delimiter? such as :

split(/:*/, $sentence); #which should return 24


Replies are listed 'Best First'.
Re: how to split a string and take certain position?
by mr_mischief (Monsignor) on Sep 24, 2007 at 04:13 UTC
    Your syntax on the first is nearly right.
    my $sentence = "5:83:24"; print ( ( split /:/, $sentence )[1] ); # prints 83
    Your second one can be done as:
    print ( ( split /:/, $sentence )[-1] );
    These both use what are known as slices. There's info on slices in perlintro, although I think that just deals with slices of named arrays. List slices specifically are in perldata. Lots of PM nodes have examples of slices as well.

    Slices can be a little tricky at first, and getting print to see your slice as a single item to print is another issue. Printing the evaluation of the slice accounts for the need to use the outer set of parentheses here.

Re: how to split a string and take certain position?
by mreece (Friar) on Sep 24, 2007 at 04:26 UTC
    sure, if you group your parens appropriately -- (split)[$i]:

      print( (split(/:/, $sentence))[1] );  # negative indexes work too

    or the less cluttered: print +(split /:/, $sentence)[1];

    for the latter question, consider: ($match) = $sentence =~ /([^:]+)$/

Re: how to split a string and take certain position?
by adrive (Scribe) on Sep 24, 2007 at 06:05 UTC
    "for the latter question, consider: "
    ($match) = $sentence =~ /([^:]+)$/
    "
    I can never really understand how string matching works :P
    why do we need a $ at the end to work? i mean, isn't + already = "One or more of the last character"

      Your confusing two definitions of "last": "previous" and "ending".

      + means "one or more of the previous atom".
      $ (without the m option) means "end of string, or a line feed followed by the end of string".

Re: how to split a string and take certain position?
by adrive (Scribe) on Sep 24, 2007 at 04:28 UTC
    whoah , i didn't know it worked. print doesn't seem to work if i don't use my split functions enclosed in a bracket. But thanks :)
      There's a general rule: If it looks like a sub-call, it's a sub-call. :-)

        print (@somelist)[1]

      is treated as

        print(@somelist) [1]

      which is also a syntax error.

      -David

        Two common solutions is two put parens around the function's arguments

        print( (@somelist)[1] );

        Or put something between the function name and the opening parens, such as the no-op operator

        print +(@somelist)[1];

Log In?
Username:
Password:

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

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

    No recent polls found