You could use a temporary variable to keep the last value.
>my $temp;
>foreach my $elem (@long_list) {
> my $k="junk";
> foreach $k (keys %hash) {
> $temp = $k;
> last if($elem =~ /$k/); ## ok, I've got the $k I need
> }
> $hash{$temp}->{'foo'} = 42; ## now it works
>}
-----Original Message-----
From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, February 19, 2002 1:14 PM
To: Bradford Ritchie
Cc: [EMAIL PROTECTED]
Subject: Re: foreach() index scoping question
On Feb 19, Bradford Ritchie said:
>foreach my $elem (@long_list) {
> my $k="junk";
> foreach $k (keys %hash) {
> last if($elem =~ /$k/); ## ok, I've got the $k I need
> }
> $hash{$k}->{'foo'} = 42; ## oops, $k is reset to "junk"
>}
That behavior is documented, yes.
Perhaps you want to do:
for my $elem (@long_list) {
for my $k (keys %hash) {
if ($elem =~ $k) {
$hash{$k}{foo} = 42;
last;
}
}
}
>Or, is there any way to get around the way foreach localizes the index
>variable?
No. It does that on purpose, and is documented to do so.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
----------------------------------------------------------------------------
--------------------
The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]