RSS Forums RSS
Please support our Perl advertiser: Programming Forums
Views: 829 | Replies: 4 | Solved
Reply
Join Date: Jan 2008
Posts: 37
Reputation: rysin is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rysin's Avatar
rysin rysin is offline Offline
Light Poster

Program Problem

  #1  
Apr 23rd, 2008
How do you add to a variable that is inside a foreach loop? Everything I try hasnt worked.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 337
Reputation: eggi is on a distinguished road 
Rep Power: 2
Solved Threads: 32
eggi eggi is offline Offline
Posting Whiz

Re: Program Problem

  #2  
Apr 23rd, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Reply With Quote  
Join Date: Jan 2006
Posts: 223
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 20
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Program Problem

  #3  
Apr 23rd, 2008
Originally Posted by rysin View Post
How do you add to a variable that is inside a foreach loop? Everything I try hasnt worked.

Post your code.
challenge the limits
Reply With Quote  
Join Date: Mar 2006
Posts: 660
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 38
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: Program Problem

  #4  
Apr 24th, 2008
Originally Posted by katharnakh View Post
Post your code.


Ditto. Post your code.
Reply With Quote  
Join Date: Jan 2008
Posts: 37
Reputation: rysin is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rysin's Avatar
rysin rysin is offline Offline
Light Poster

Re: Program Problem

  #5  
Apr 24th, 2008
I found the problem thanks for all the help!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:40 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC