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

I am using merlyn's/perlmonkey's solution on how to match comments and quotes. When I do the following:
%hash = ('"', '"', '/*', '*/', '<-', '->'); foreach (keys(%hash)) { $start = $_; $end = $hash{$_}; my $inside = 0; my $oldpos = 0; while ($string =~ /(?<!\\)(\Q$start\E|\Q$end\E)/g) { if ($inside == 0) { $oldpos = pos($string) - length($start); $inside++; } else { push (@a, substr($string, $oldpos, pos($string)-$oldpos)); $inside--; } } }
the code works and @a gets all the matches.
But when I create my %hash differently, like:
while($db->FetchRow()) { undef %mult; %mult = $db->DataHash(); push (@multy, values(%mult)); } %hash = reverse(@multy);
and then run the same code as above, my $string gets split by character, and each element of @a receives a single character.
What's the difference and how do I fix that?

Replies are listed 'Best First'.
Re: strange hash behavior
by perlmonkey (Hermit) on May 27, 2000 at 10:35 UTC
    print out the values of @multy before you reverse it. I bet it is not what you expect. Your database routine is probably not returning any values.
    You can reproduce your problem by setting %hash to a bunch of empty strings:%hash = ('', '', '', '');
      No, prints everything corectly in the reverse order.
        I got. You were right. my hash looked like ('/*', '*/', '','')
        Thanks.