On Wed, Aug 4, 2010 at 11:36, Mike Martin <[email protected]> wrote:
snip
> This fails but if I replace
> $type_g=$options{$key}->[4] if $chk=~/$type/
>
> with either
> $type_g=$options{$key}->[4] if $type=~/$chk/; (ie:reversing the match)
>
> or
>
> $type_g=$options{$key}->[4] if $type eq $chk;
>
> any idea on the reasons for this behaviour
snip
This is just a guess because you have not told us what $chk holds, but
I strongly suspect that $chk contains more than just "val". It
probably has a newline or some other characters in it: "val\n"
contains the substring "val", but "val" does not contain the substring
"val\n". You may find it helpful to add the following line to your
script for debugging purposes:
warn join ", ", map { "[$_]: " . ord } split //, $chk;
This will split $chk into characters and then map those characters
into a string that contains the character surrounded by brackets
followed by a colon and the characters Unicode position (which is
identical to ASCII for the first 127 characters). Knowing the Unicode
position is important because there are many unprintable characters
like Unicode position 0 (ASCII null character) and Unicode position 8
(ASCII backspace character).
On a related note, you most likely have a bug with the placement of
the variable $type_g and the print statement. You are only printing
$type_g after the loop, so you will print the value of $type_g for the
last key (and which key is last is not guaranteed by Perl 5) that has
a match. This may be your intended behavior, but it looks odd to me.
I would expect it to look like this:
my $type='val';
for my $key (keys %options) {
if ($options{$key}[3] =~ /$type/) {
print "\n$type\n$options{$key}[4]\n";
}
}
or this
my $type='val';
my @type_g;
for my $key (keys %options) {
if ($options{$key}[3] =~ /$type/) {
push @type_g, $options{$key}[4];
}
}
print "\n$typ...@type_g\n";
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/