$_ not changing in a foreach

S

Sam

Hello
foreach (@calc) {
$total += $_;
}
DB<70> p $_
is giving the same value for each loop of the code above. should itn't
change its value to each item in the array @calc or I am missing somthing.

any idea or hints...thanks alot
 
S

Sam

Sam said:
Hello
foreach (@calc) {
$total += $_;
}
DB<70> p $_
is giving the same value for each loop of the code above. should itn't
change its value to each item in the array @calc or I am missing somthing.

any idea or hints...thanks alot

I fixed it
it seams that in a previous line I had
my @list = @{ shift(@_) };
my $value = shift(@_);
so I changed that to
my @list = @{ shift() };
my $value = shift();
maybe the first case when I used @_ that set perl default variable $_ to
a fixed value but when I removed @_ that freed it again.
if I am not correct please correct me and explain why that had happend.

thanks
 
B

Bob Walton

I'm not sure exactly what you are trying to say.

When the foreach is done, the value of $_ seems

to revert to the value it had prior to the foreach

(but I don't think that is guaranteed or documented).


Example:

@calc=(2,4,6,8);
$_=3;
for(@calc){
$total+=$_;
}
print "value is $_, total is $total\n";

prints "value is 3, total is 20" (on Windoze 98SE w/AS build 806).

$_ is aliased to the successive elements of @calc

as the loop progresses. You should not expect $_

to be set to the last element of the array when

the loop terminates normally. Is that what you

were expecting?

I fixed it
it seams that in a previous line I had
my @list = @{ shift(@_) };
my $value = shift(@_);
so I changed that to
my @list = @{ shift() };
my $value = shift();
maybe the first case when I used @_ that set perl default variable $_ to
a fixed value but when I removed @_ that freed it again.
if I am not correct please correct me and explain why that had happend.

thanks

As to the above comments, I'm afraid they make no sense to me at all.
What relation does @list and $value have to the foreach loop code? None
that's discernable to me. Just as @_ has nothing to with $_. There
should be no difference between "shift()" and "shift(@_)", since @_ is
the default argument for shift if the argument is omitted. It is
unclear what you were expecting and why you were expecting it. Could
you try again with a clear description, such as a short complete
standalone program anyone can copy/paste/execute (like the one I put in
above) that will illustrate your problem?
 
T

Tad McClellan

Bob Walton said:
When the foreach is done, the value of $_ seems

to revert to the value it had prior to the foreach

(but I don't think that is guaranteed or documented).


Sure it is, in the first paragraph of the description for
"Foreach Loops" in perlsyn.pod no less:

... the variable is implicitly local to the loop and regains its
former value upon exiting the loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,362
Latest member
OrderTrimKetoBoost

Latest Threads

Top