The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with Stop Script during shutdown of o/s shipon_97 Red Hat 1 08-11-2009 10:27 AM
pERL SCRIPT FOR MONITORING DATE/TIME STAMPS FOR START AND STOP OF APPLICAION IN A LOG FREDDIE091970 Shell Programming and Scripting 4 03-31-2008 09:59 PM
How to stop perl service rainbow_bean AIX 4 01-03-2008 03:07 PM
how to stop others users to stop viewing what i am doing ? mobile01 UNIX for Advanced & Expert Users 5 12-04-2006 08:37 AM
ksh/Linux: Variable scoping issue? Pl. help! jasmeet100 Shell Programming and Scripting 2 09-30-2005 06:35 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 5 Days Ago
thmnetwork thmnetwork is offline
Registered User
  
 

Join Date: Mar 2004
Location: Boise, Idaho, United States
Posts: 39
I can't stop the scoping problem (perl)

I hate to post something so n00bish, but I'm pretty n00bish when it comes to perl so here it goes:

Code:

$var=1;

while(1){ 

   if ($var == 2){
      last;
   }

   $var=2;
}
works the way I intend it to, breaking the infinite loop after the first time through, but:

Code:
while(1){

   $var=1;

   if ($var == 2){
      last;
   }

   $var=2;

}
results in what I can only assume is an infinite loop. My question is what changes scope-wise to the if statement when $var is instantiated outside of the while loop?

update:
Title is supposed to be "spot" not "stop" but apparently I'm dyslexic.

Last edited by thmnetwork; 5 Days Ago at 12:15 AM..
  #2 (permalink)  
Old 5 Days Ago
daptal daptal is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 60
Code:
$var=1;

while(1){ 

   if ($var == 2){
      last;
   }

   $var=2;
}
In the above
1) step 1 var =1 then goes to while loop
2) In the while loop in the first check var != 2 so doesnot get into the if loop then sets var = 2
3) In the next attempt var is 2 goes into if loop and the while loop ends

Now lets check the second block of code
Code:
while(1){

   $var=1;

   if ($var == 2){
      last;
   }

   $var=2;

}
1) goes into the while loop then sets var = 1 and checks if var == 2 so doesnot enter into the if loop
2) sets var =2 and then again goes to the while loop begining

3) Now here what is happening is it sets the variable var to 1 again . the condition of var=2 is never met. Hence the infinite loop.

Regarding the scope in the first instance if you print var outside the while loop it would be 1
in the second instance depending on where you declared the variable var like my $var will determine the value.

HTH,
PL
  #3 (permalink)  
Old 4 Days Ago
thmnetwork thmnetwork is offline
Registered User
  
 

Join Date: Mar 2004
Location: Boise, Idaho, United States
Posts: 39
thanks, I don't know how I missed that, I was running on fumes at that point of the night. Thanks for indulging me.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0