Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

problems splitting string

by Anonymous Monk
on Dec 15, 2005 at 15:55 UTC ( [id://516987]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I am having problems spliting the below:
$string = '1 Cat 2 Dogs 3 Hamsters';
I want to split the string, so that i have 1 Cat, 2 Dogs, 3 hamsters pushed onto an array? Thanks

Replies are listed 'Best First'.
Re: problems splitting string
by VSarkiss (Monsignor) on Dec 15, 2005 at 16:05 UTC
      thanks for the help
Re: problems splitting string
by merlyn (Sage) on Dec 15, 2005 at 17:19 UTC
    As illustrated by some of the answers, you can see that this is where "split" is probably the wrong tool.
    • When it's easier to talk about what to throw away, use split.
    • When it's easier to talk about what to keep, use a match-global.
    Don't reach for the wrong tool. In this case, it was easier to talk about what to keep.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      For the split bullet, I would actually describe it as "When it's easier to talk about separators" - such as "I want to split on commas" or "I want to separate on colons" or "I want to separate right before each number (not necessarily each digit)".

      If this is how you're thinking of the problem, then split is probably the right tool. However, given the original question, I get the feeling that the problem was being thought of as "what to keep" where using match-global is more in line with the real problem. The OP seems to be asking "How do I do X with Y?" - wanting to match stuff with split.

      If you take away the context of the question, and it were to become "How do I get this string '1 Cat 2 Dogs 3 Hamsters' to become this list ('1 Cat', '2 Dogs', '3 Hamsters')", then I would think either split or match-global would be perfectly legitimate responses.

Re: problems splitting string
by mrborisguy (Hermit) on Dec 15, 2005 at 16:04 UTC

    What I was thinking would be to have a look ahead for a number.

    my $string = '1 Cat 2 Dogs 3 Hamsters'; my @animals = split /(?=\d)/, $string;

        -Bryan

        Yep. Good catch, I noticed that, and was changing it, then I noticed you posted. I tried to update with a solution using map to take off the trailing space, but it seems VSarkiss' solution is similar to mine but more elegant, so just refer down a few posts.

            -Bryan

Re: problems splitting string
by Hue-Bond (Priest) on Dec 15, 2005 at 16:04 UTC

    This will do the trick:

    my $string = '1 Cat 2 Dogs 3 Hamsters'; my @a = $string =~ m/\b\d+\s+\w+\b/g; use Data::Dumper; print Dumper \@a; __END__ $VAR1 = [ '1 Cat', '2 Dogs', '3 Hamsters' ];

    Update: if you want to push, try this:

    my $string = '1 Cat 2 Dogs 3 Hamsters'; my @a; push @a, $1 while $string =~ m/(\b\d+\s+\w+\b)/g; use Data::Dumper; print Dumper \@a; __END__ $VAR1 = [ '1 Cat', '2 Dogs', '3 Hamsters' ];

    --
    David Serrano

      The trailling \b is useless, and your your code won't process "5 Golden Rings 4 Calling Birds"
        Why not? It won't be sorted in the way you assume would be required, but were never explicitly requested.

Log In?
Username:
Password:

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

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

    No recent polls found