0

How do you add to a variable that is inside a foreach loop? Everything I try hasnt worked.

4
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by rysin
0

Hey There,

Are you having problems modifying a variable you declare outside of the loop?

This code will increment the variable in the foreach loop and change it's value outside the loop.

Let me know if I'm misunderstanding you :)

Best Wishes,

Mike

#!/usr/bin/perl

$fancy=0;

print "BEFORE $fancy\n";

@loop = qw(1 2 3 4 5 6 7);

foreach $loop (@loop)   {
        $fancy++;
}

print "AFTER $fancy\n";

I called it "poil" and this is the output below

-bash-3.2$ ./poil
BEFORE 0
AFTER 7
0

How do you add to a variable that is inside a foreach loop? Everything I try hasnt worked.

Post your code.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.