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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.