Re: Using $variable outside a foreach loop

2011-06-03 Thread 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

Re: Using $variable outside a foreach loop

2011-06-03 Thread shawn wilson
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

Re: Using $variable outside a foreach loop

2011-06-03 Thread Uri Guttman
> "R" == Ruud writes: R> On 2011-06-03 17:37, sono...@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: >> >>

Re: Using $variable outside a foreach loop

2011-06-03 Thread Dr.Ruud
On 2011-06-03 17:37, sono...@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

Re: Using $variable outside a foreach loop

2011-06-03 Thread 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/

Re: Using $variable outside a foreach loop

2011-06-03 Thread Shawn H Corey
On 11-06-03 03:58 PM, sono...@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? E

Re: Using $variable outside a foreach loop

2011-06-03 Thread sono-io
On Jun 3, 2011, at 10:22 AM, Uri Guttman wrote: >> 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. Thanks, Uri. I didn't catch that.

Re: Using $variable outside a foreach loop

2011-06-03 Thread 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

Re: Using $variable outside a foreach loop

2011-06-03 Thread Brian Fraser
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 ( ... ) { ... } Wi

Re: Using $variable outside a foreach loop

2011-06-03 Thread 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)) {

Re: Using $variable outside a foreach loop

2011-06-03 Thread Shawn H Corey
On 11-06-03 11:37 AM, sono...@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

Re: Using $variable outside a foreach loop

2011-06-03 Thread 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

Re: Using $variable outside a foreach loop

2011-06-03 Thread C.DeRykus
On Jun 3, 8:37 am, sono...@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". > One option is an outer enclosing block that'll extend the scope of $name to that entire block:

Re: Using $variable outside a foreach loop

2011-06-03 Thread Jim Gibson
At 8:37 AM -0700 6/3/11, sono...@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 (/,

Re: Using $variable outside a foreach loop

2011-06-03 Thread 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. > 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 c

Re: Using $variable outside a foreach loop

2011-06-03 Thread 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 un

Using $variable outside a foreach loop

2011-06-03 Thread 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.=:\ Here's the loop: foreach my $name (split (/, */, $names)) { next unle