in reply to Parsing String

Split is an interesting idea. ...this ought to do it:

my $results = "Home Town, Log in activity: 57 DI activity: 13 FA activ +ity: 13 FAI activity: 4 Login activity: 23 Not Found activity: 4"; my @numbers = split /\D+/, $results; print "$_\n" for @numbers;

If you want to do it with an RE instead, just use the /g modifier in list context like this:

my $results = "Home Town, Log in activity: 57 DI activity: 13 FA activ +ity: 13 FAI activity: 4 Login activity: 23 Not Found activity: 4"; my @numbers = $results =~ m/\d+/g; print "$_\n" for @numbers;


Dave

Replies are listed 'Best First'.
Re: Re: Parsing String
by Not_a_Number (Prior) on Apr 21, 2004 at 19:03 UTC
    my @numbers = split /\D+/, $results;

    The problem with this is that it creates an extra empty element at the beginning of the @numbers array. Go for the RE solution.

    just another dave

Re: Parsing String
by Belgarion (Chaplain) on Apr 21, 2004 at 18:44 UTC

    Dave, thank you for pointing out the /g modifier! I had complete forgotten about that fact when I posted my answer.

      I couldn't get it to work I am trying to go item by item using reg. expressions.
        Well, here's a method that iterates over each of the items found:

        my $results = "Home Town, Log in activity: 57 DI activity: 13 FA activ +ity: 13 FAI activity: 4 Login activity: 23 Not Found activity: 4"; foreach my $number ( $results =~ m/\d+/g ) { print "$number\n"; }


        Dave

        I guess I don't understand what you're trying to do then. davido's code pulls out all the numbers into an array that you can the loop over and do whatever you like with. What exactly are you trying to do with your code?