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

I have two strings that are identically formatted and will each pass the same regex. I am interested in how to get the below code working with the '/g' modifier.

$s1 = "45 20 00 00 00 03 00"; $s2 = "40 20 00 00 00 00 00"; while ((@a = $s1 =~ /(\d{2})/g) && (@b = $s2 =~ /(\d{2})/g)){ print "found $a[0] and $b[0] !!!\n"; }
It seems to not be starting back at the last position found as the loop continues indefinitely and only gets the first 2 values.
found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! found 45 and 40 !!! ... continues until i break

Any ideas?

Thanks!

Replies are listed 'Best First'.
Re: Two Regex Searches Simultaneously (on One Line)
by GrandFather (Saint) on Oct 24, 2010 at 00:24 UTC

    @a puts $s1 =~ /(\d{2})/g in list context so all matches will be found and used to populate @a. You probably want something like:

    #!/usr/bin/perl use warnings; use strict; my $s1 = "45 20 00 00 00 03 00"; my $s2 = "40 20 00 00 00 00 00"; my @a1 = $s1 =~ /(\d{2})/g; my @a2 = $s2 =~ /(\d{2})/g; die "Mismatched lists" if @a1 != @a2; print "found $a1[$_] and $a2[$_]\n" for 0 .. $#a1;

    Prints:

    found 45 and 40 found 20 and 20 found 00 and 00 found 00 and 00 found 00 and 00 found 03 and 00 found 00 and 00
    True laziness is hard work
Re: Two Regex Searches Simultaneously (on One Line)
by CountZero (Bishop) on Oct 24, 2010 at 00:29 UTC
    Consider:
    use strict; use warnings; use 5.012; use Data::Dumper; my $s1 = '45 20 00 00 00 03 00'; my $s2 = '40 20 00 00 00 00 00'; while ((my @a = $s1 =~ /(\d{2})/g) && (my @b = $s2 =~ /(\d{2})/g)){ say "found $a[0] and $b[0] !!!"; say Dumper(\@a, \@b); }
    A regex in list context will give you all matches at once.

    Next time the loop comes around, it starts all over again as it has exhausted its matching and resets.

    This code will do what you want:

    use strict; use warnings; use 5.012; use Data::Dumper; use List::MoreUtils qw/each_array/; my $s1 = '45 20 00 00 00 03 00'; my $s2 = '40 20 00 00 00 00 00'; my @first = $s1 =~ /(\d{2})/g; my @second = $s2 =~ /(\d{2})/g; my $ea = each_array(@first, @second); while (my ($first, $second) = $ea->()){ say "found $first and $second !!!"; }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Two Regex Searches Simultaneously (on One Line)
by TomDLux (Vicar) on Oct 24, 2010 at 14:20 UTC

    Regular expressions are great for handling complex string manipulations, but in this case, I would say you are taking a string of numbers separated by spaces, and dividing on the spaces. split() seems a more natural mechanism:

    my @a = split ' ', $s1; my @b = split ' ', $s2;

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      split() is also the fastest method i think when compiled if your doing large scale searches.
Re: Two Regex Searches Simultaneously (on One Line)
by SomeNetworkGuy (Sexton) on Oct 24, 2010 at 00:47 UTC
    I understand my folly now. Thanks guys!