hi

i have some difficulty with a rather simple loop and simple RegExp:


please try the following code two times - one with no command line arguments and one with a true argument [the latter call will reverse the sorting of the hash keys]


for example:

$ net.pl
$ net.pl 1


the funny thing is that even that perl guarantees me that the two strings $str and str2 are equal (which they should be since i assign them to each other just before the "eq" test), the RegExp only match when the keys in the hash are alphabetically sorted


can anybody explain this ?

thanks
./allan

#######################################
use strict;

my %maps  = (
    i => 'somestring',
    j => 'someother',
);


my $str = q( I:somestring J:someother );

foreach my $key (sort by_str keys %maps ) {
    my $str2 = $str;

if ($str eq $str2) {
print "\n\tGoing to testing the key $key\n\tApparently the varibale \$str is identical to \$str2\n\n";


    }

    print "Testing \$str ...\n";
    if ($str =~ m/^$key.+/mig) {
        print "\tA match for \$str\n";
    } else {
        print "\tHmm no match in \$str\n";
    }

    print "Testing \$str2 ...\n";
    if ($str2 =~ m/^$key.+/mig) {
        print "\tA match for \$str2\n";
    } else {
        print "\tHmm no match in \$str2\n";
    }
}

sub by_str {
    if ($ARGV[0]) {
        ($b cmp $a)
    } else {
        ($a cmp $b)
    }
}






Reply via email to