Using $variable outside a foreach loop

Using $variable outside a foreach loop

am 03.06.2011 17:37:26 von sono-io

Maybe it's too early in the morning here, but I can't seem to =
remember how to use a lexical $variable that is defined inside a foreach =
loop, outside of that loop. =3D:\

Here's the loop:

foreach my $name (split (/, */, $names)) {
next unless ($name =3D~ /\w/);
my $sortstr =3D substr("00$totalcells", -2);
$html =3D~ s|\n $cell{"$sortstr$name"} =3D "";
$cell{"$sortstr$name"} =3D~ =
s/\[NAME\]/$name/sig;
$totalcells++;
}

I want to use "$name" in another loop just after this one, but =
when I do, I get "Global symbol $name requires explicit package".

Could someone please point me in the right direction?

Thanks,
Marc=

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 17:45:05 von Alan Haggai Alavi

Hello,

> foreach my $name (split (/, */, $names)) {

Here, the scope of $name is limited to the foreach loop and not outside it.
So, you will have to declare the variable again for use outside the loop.

Regards,
Alan Haggai Alavi.
--
The difference makes the difference.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 17:56:06 von sono-io

On Jun 3, 2011, at 8:45 AM, Alan Haggai Alavi wrote:

> Here, the scope of $name is limited to the foreach loop and not =
outside it.=20
> So, you will have to declare the variable again for use outside the =
loop.

But wouldn't that make the second "$name" a different variable? =
I'm not at my computer to try it.

Marc=

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 18:23:02 von Jim Gibson

At 8:37 AM -0700 6/3/11, sono-io@fannullone.us wrote:
> Maybe it's too early in the morning here, but I can't seem to
>remember how to use a lexical $variable that is defined inside a
>foreach loop, outside of that loop. =:\
>
> Here's the loop:
>
> foreach my $name (split (/, */, $names)) {
> next unless ($name =~ /\w/);
> my $sortstr = substr("00$totalcells", -2);
> $html =~ s|\n > $cell{"$sortstr$name"} = "";
> $cell{"$sortstr$name"} =~ s/\[NAME\]/$name/sig;
> $totalcells++;
> }
>
> I want to use "$name" in another loop just after this one,
>but when I do, I get "Global symbol $name requires explicit package".

Declare the variable just before the loop, and remove the 'my' from
the foreach statement:

my $name;
foreach $name ( ... ) {
...
}


--
Jim Gibson
Jim@Gibson.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 18:30:08 von derykus

On Jun 3, 8:37=A0am, sono...@fannullone.us wrote:
> ...
> =A0 =A0 =A0 =A0 I want to use "$name" in another loop just after this one=
, but when I do, I get "Global symbol $name requires explicit package".
>

One option is an outer enclosing block that'll
extend the scope of $name to that entire block:

{ # enclosing block
my $name;
for $name ( split //... ) { } # first loop
for $name ( ... ) { } # next loop
}

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 19:08:45 von sono-io

C.DeRykus wrote:

> One option is an outer enclosing block that'll extend the scope of =
$name to that entire block:

Jim Gibson wrote:

> Declare the variable just before the loop, and remove the 'my' from =
the foreach statement:


Thanks for the responses. I was able to get it to work.

Marc=

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 19:19:02 von Shawn H Corey

On 11-06-03 11:37 AM, sono-io@fannullone.us wrote:
> I want to use "$name" in another loop just after this one, but when I do, I get "Global symbol $name requires explicit package".
>
> Could someone please point me in the right direction?

Certainly. The variable used in a foreach loop is independent of
anything outside the loop. Even if it's declared before the loop, it's
value is unavailable inside the loop:

#!/usr/bin/env perl

use strict;
use warnings;

my $name = 'forgotten';

for $name ( qw( fred barney wilma )){
print "\$name is $name\n";
}

print "\$name is $name\n";
__END__

Outside the loop, it becomes just another variable.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 19:22:09 von Uri Guttman

>>>>> "s" == sono-io writes:

s> Maybe it's too early in the morning here, but I can't seem to remember how to use a lexical $variable that is defined inside a foreach loop, outside of that loop. =:\
s> Here's the loop:

s> foreach my $name (split (/, */, $names)) {
s> next unless ($name =~ /\w/);
s> my $sortstr = substr("00$totalcells", -2);

that looks like you are forcing leading 0's. the common idiom for that
is sprintf "%02d", $totalcells. your way works but is harder to read.

s> $html =~ s|\n
why are you doing that insert of a newline each time in that loop? you
don't use or modify $html anywhere else in this loop.

s> $cell{"$sortstr$name"} = "";
s> $cell{"$sortstr$name"} =~ s/\[NAME\]/$name/sig;

you just assigned that value the line before. unless $name has [NAME]
inside it, how could that s/// op ever do anything?

s> $totalcells++;
s> }

s> I want to use "$name" in another loop just after this one, but when I do, I get "Global symbol $name requires explicit package".

did you want to use $name as the loop variable again? that is perfectly
fine as this $name is scoped only to that loop. if you just want to use
$name for any other reason you need to just declare it with my.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 21:03:29 von Brian Fraser

--0016e6d7dfcbc3b15304a4d368b2
Content-Type: text/plain; charset=UTF-8

On Fri, Jun 3, 2011 at 1:23 PM, Jim Gibson wrote:

> Declare the variable just before the loop, and remove the 'my' from the
> foreach statement:
>
> my $name;
> foreach $name ( ... ) {
> ...
> }
>

That won't do. What that code actually translated to is
my $name;
for my $name ( ... ) { ... }

With the second $name masking the first! IIRC this is explained somewhere in
PBP, but I don't have the book at hand right now. In any case, if you want
to use the variable outside of the for, don't name the two variables the
same.
The rough rule of thumb is that leaving off the my in a for(each) is
probably introducing subtle bugs into your program.

Brian.

--0016e6d7dfcbc3b15304a4d368b2--

Re: Using $variable outside a foreach loop

am 03.06.2011 21:15:47 von Uri Guttman

>>>>> "BF" == Brian Fraser writes:

BF> On Fri, Jun 3, 2011 at 1:23 PM, Jim Gibson wrote:
>> Declare the variable just before the loop, and remove the 'my' from the
>> foreach statement:
>>
>> my $name;
>> foreach $name ( ... ) {
>> ...
>> }
>>

BF> That won't do. What that code actually translated to is
BF> my $name;
BF> for my $name ( ... ) { ... }

not true.

perl -le 'my $x = "zzz" ; for $x ( qw( foo bar ) ) { print "L: $x" } print "E: $x"'
L: foo
L: bar
E: zzz

foreach without a my will localize the loop variable. that isn't the
same as my.

BF> With the second $name masking the first! IIRC this is explained
BF> somewhere in PBP, but I don't have the book at hand right now. In
BF> any case, if you want to use the variable outside of the for,
BF> don't name the two variables the same. The rough rule of thumb is
BF> that leaving off the my in a for(each) is probably introducing
BF> subtle bugs into your program.

why search PBP when the perldocs are very clear about it and easily
searched?

from perldoc perlsyn:

The "foreach" loop iterates over a normal list value and sets
the variable VAR to be each element of the list in turn. If the
variable is preceded with the keyword "my", then it is lexically
scoped, and is therefore visible only within the loop.
Otherwise, the variable is implicitly local to the loop and
regains its former value upon exiting the loop. If the variable
was previously declared with "my", it uses that variable instead
of the global one, but it's still localized to the loop. This
implicit localisation occurs only in a "foreach" loop.

so in my oneliner the my allowed the $x to not need redeclaring in the
foreach but it still localized the value to the loop.

but as i said in another post, the OP just likely wants to use $name as
a loop var again which he can with another my declare.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 21:39:35 von rvtol+usenet

On 2011-06-03 17:37, sono-io@fannullone.us wrote:

> Maybe it's too early in the morning here, but I can't seem to remember how to use a lexical $variable that is defined inside a foreach loop, outside of that loop. =:\
>
> Here's the loop:
>
> foreach my $name (split (/, */, $names)) {
> next unless ($name =~ /\w/);
> my $sortstr = substr("00$totalcells", -2);
> $html =~ s|\n > $cell{"$sortstr$name"} = "";
> $cell{"$sortstr$name"} =~ s/\[NAME\]/$name/sig;
> $totalcells++;
> }
>
> I want to use "$name" in another loop just after this one, but when I do, I get "Global symbol $name requires explicit package".
>
> Could someone please point me in the right direction?

What would be the use of that?

If you declare $name before the loop, it will (after leaving the loop)
have the last value assigned to it. What would you want that for?

To me, yours is an X-Y problem. (see google)

You should probably just declare a new variable for the other loop.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 21:58:36 von sono-io

On Jun 3, 2011, at 10:22 AM, Uri Guttman wrote:

>> s> my $sortstr =3D substr("00$totalcells", -2);
> that looks like you are forcing leading 0's. the common idiom for that
> is sprintf "%02d", $totalcells. your way works but is harder to read.

Thanks, Uri. I didn't catch that.

I didn't write this script. I'm using it to learn from and =
cleaning it up at the same time. I know that learning Perl from an =
older script probably isn't the best way to do it, but since I have a =
vested interest in how this script works, it's forcing me to learn it =
faster than I would otherwise. However, if anyone knows of a shopping =
cart written in modern perl, I'd love to have a look.

>> s> $html =3D~ s|\n > why are you doing that insert of a newline each time in that loop? you
> don't use or modify $html anywhere else in this loop.=20

Actually, it's being used on an HTML template and just makes the =
HTML code look cleaner.

>> s> I want to use "$name" in another loop just after this one, but =
when I do, I get "Global symbol $name requires explicit package".
> did you want to use $name as the loop variable again? that is =
perfectly
> fine as this $name is scoped only to that loop. if you just want to =
use
> $name for any other reason you need to just declare it with my.

I wasn't very clear in what I wanted. Sorry. I wanted to use =
the value of $name in another loop but after testing, it looks like =
Shawn was correct when he wrote:

> The variable used in a foreach loop is independent of anything outside =
the loop.

I actually had to use another variable in the second loop to =
grab the name.

Thanks to everyone who responded. I got it to work now.

Marc=

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 22:38:11 von Shawn H Corey

On 11-06-03 03:58 PM, sono-io@fannullone.us wrote:
> I wasn't very clear in what I wanted. Sorry. I wanted to use the value of $name in another loop but after testing

That implies something is wrong with your logic. The question is: what
value of $name do you want? The first? The last? Either you want the
second loop nested in the first, or you want a completely independent
one that iterates through all the values.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 03.06.2011 23:13:32 von sono-io

On Jun 3, 2011, at 1:38 PM, Shawn H Corey wrote:

> That implies something is wrong with your logic.

Yep. I came to the same conclusion. =:)

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 04.06.2011 01:49:20 von Uri Guttman

>>>>> "R" == Ruud writes:

R> On 2011-06-03 17:37, sono-io@fannullone.us wrote:
>> Maybe it's too early in the morning here, but I can't seem to remember how to use a lexical $variable that is defined inside a foreach loop, outside of that loop. =:\
>>
>> Here's the loop:
>>
>> foreach my $name (split (/, */, $names)) {
>> next unless ($name =~ /\w/);
>> my $sortstr = substr("00$totalcells", -2);
>> $html =~ s|\n >> $cell{"$sortstr$name"} = "";
>> $cell{"$sortstr$name"} =~ s/\[NAME\]/$name/sig;
>> $totalcells++;
>> }
>>
>> I want to use "$name" in another loop just after this one, but when I do, I get "Global symbol $name requires explicit package".
>>
>> Could someone please point me in the right direction?

R> What would be the use of that?

R> If you declare $name before the loop, it will (after leaving the loop)
R> have the last value assigned to it. What would you want that for?

not true. see a post i made with a one liner. $name is localized (if not
my) so the previous value before the loop is seen after the loop. loop
values don't affect the outer $name at all.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Using $variable outside a foreach loop

am 04.06.2011 02:00:29 von Shawn Wilson

--0022158df337ef2f3b04a4d78e18
Content-Type: text/plain; charset=ISO-8859-1

On Jun 3, 2011 3:17 PM, "Uri Guttman" wrote:
>

>
> perl -le 'my $x = "zzz" ; for $x ( qw( foo bar ) ) { print "L: $x" } print
"E: $x"'
> L: foo
> L: bar
> E: zzz
>

That's odd, I would have thought that would have given 'foo bar bar'. So,
how would you keep data from a loop once you're outside of the loop?

--0022158df337ef2f3b04a4d78e18--

Re: Using $variable outside a foreach loop

am 04.06.2011 02:07:53 von Uri Guttman

>>>>> "sw" == shawn wilson writes:

sw> On Jun 3, 2011 3:17 PM, "Uri Guttman" wrote:
>>

>>
>> perl -le 'my $x = "zzz" ; for $x ( qw( foo bar ) ) { print "L: $x" } print
sw> "E: $x"'
>> L: foo
>> L: bar
>> E: zzz

sw> That's odd, I would have thought that would have given 'foo bar bar'. So,
sw> how would you keep data from a loop once you're outside of the loop?

you don't use a loop variable for that. just declare it before the loop,
assign as needed inside the loop and it will be there. if that gets
messy as it can, i change the code to use a sub with the loop inside and
return the value.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/