in reply to problems splitting string

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

Replies are listed 'Best First'.
Re^2: problems splitting string
by ikegami (Patriarch) on Dec 15, 2005 at 16:12 UTC
    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.
        The problem is that it won't match multiple words.

        Caution: Contents may have been coded under pressure.