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

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

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

Post your code.

Post your code.

Ditto. Post your code.

I found the problem thanks for all the help!