CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
$_ bug

 



Nukem123
Novice

Nov 3, 2010, 9:39 PM

Post #1 of 5 (621 views)
$_ bug Can't Post

Wanted to ask you why in the second loop does not go through an array from start to end.
Instead. $_ is pointing to the last element of my array throughout the second loop.

Thank you so much 

=======================

Code
 
use strict;
use warnings;

my @array = (0..99);
#my @rev_array = undef;

my $counter = 0;


foreach $counter(@array)
{

$_ = $counter; #assign counterís value to whichever array element $_ is currently pointing to
print 'the first $_ is: '.$_."\n";
$counter++; #increment the counter
print 'the first $counter is: '.$counter."\n";
}

#reset the counter
$counter= 0;

foreach $counter (@array)
{
print 'second $counter is: '.$counter."\n";
print 'second $_ is: '.$_."\n";
#if curr. array element is even then
if ($_ %2==0)
{
# remove it from the array referenced by $ array_ref
print 'inside the IF!\n';
$_ = undef;
}

$counter++;
}

Partial output:

second $_ is: 99
second $counter is: 97
second $_ is: 99
second $counter is: 98
second $_ is: 99
second $counter is: 99
second $_ is: 99
second $counter is: 100
second $_ is: 99


rovf
Veteran

Nov 4, 2010, 5:49 AM

Post #2 of 5 (614 views)
Re: [Nukem123] $_ bug [In reply to] Can't Post

Your loops actually modify @array. $counter is an *alias* to the respective array elements, and this is modified when you do a $counter++.

See the description of foreach in perldoc perlsyn.


BillKSmith
Veteran

Nov 4, 2010, 7:28 AM

Post #3 of 5 (610 views)
Re: [Nukem123] $_ bug [In reply to] Can't Post

A further implication of Ronald's comment is that the variable $counter (declared about line 7) is never modified. The foreach iterator variables are an exception to the usual scope rules for lexical variables.
Good Luck,
Bill


Nukem123
Novice

Nov 4, 2010, 3:12 PM

Post #4 of 5 (604 views)
Re: [BillKSmith] $_ bug [In reply to] Can't Post

with all due respect $counter is modified through out the program.

Quote
The foreach iterator variables are an exception to the usual scope rules for lexical variables.

what do you mean by that? are you talking about$_ now?


thank you


BillKSmith
Veteran

Nov 4, 2010, 9:24 PM

Post #5 of 5 (602 views)
Re: [Nukem123] $_ bug [In reply to] Can't Post


Code
use strict;  
use warnings;
use Data::Dumper;

my @array = ( 0 .. 99 );

my $counter = 'This is never use';

foreach $counter (@array) {
# $_ = $counter;

# print 'the first $_ is: ' . $_ . "\n";
$counter++;
# print 'the first $counter is: ' . $counter . "\n";
}

print Dumper( \@array, $counter );



This is your first loop without the prints or the assignment to $_. (Put it back in if you wish, it does not change anything) The initial value of $counter was changed to emphasise that it is not used.

Run this code. Every array element is incremented by one. $counter is unchanged. Contrary to what you would expect, the $counter inside the loop is a seperate variable. Each time through the loop, it is an alias for a different array element. Each time that $counter++ is executed, a diffferent array element is incremented. $_ has not special role in this code. It is a global variable.

Now that you have seen this, please reread the 'FOREACH LOOP' section of perldoc perlsyn. It explains it better than either of us has.
Good Luck,
Bill

(This post was edited by BillKSmith on Nov 5, 2010, 6:13 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives