ddanatzko has asked for the wisdom of the Perl Monks concerning the following question:

How Can I reset the internal pointer of a while loop back to the first element in the list? I'm trying to use a redo from a for loop to another for loop which has a while loop in between them, but the internal pointer of the while loop remains the same instead of starting at the beginning element again. udpate: code follows
MASK: while (my($defNet,$defKey) = each (%defMaskArray)){ print OUT "Starting new Subnet - $defNet\n"; $boundary = $defKey; # set boundary to key by default and allow fo +r it to be modified print OUT "Working: $defNet maskKey $boundary for $subCount{$defN +et} hosts\n"; my $refHandle = $subArray{$defNet}; # create a hash reference for +the subnet subHash my $hostsN = $subCount{$defNet}; # get number of hosts from subCou +nt hash HOST: for ($b = 1; $b <= $hostsN; $b++){ # track number of hosts +in Subnet so we know when to stop print OUT "Current iteration is $b\n"; NET: while (my($n, $host) = each %$refHandle){ # loop through + hosts in subnet subhash print OUT "| Subnet: ".$defNet."| Original Mask: $defKey +"."| Current Mask: ".$boundary."| IP: ".$host."| host #: $b"."\n"; TEST: for ($i = 0; $i <= 256; $i = ($i + $boundary)){ # e +valuate boundaries if ($i == $host || (($i + $boundary) - 1) == $host){ print OUT "$defNet for $host fell on a boundary fo +r mask - $boundary\n"; if ($boundary == 256){ print OUT "Current Sub: $defNet Mask: 256\n"; push (@routeArray, $defNet."/24") } else{ $boundary = ($boundary * 2); print OUT "test for $host failed - retrying wi +th $boundary after $b hosts attempted - original was $defKey\n"; $i = 0; redo HOST; } } elsif ($i > $host){ print OUT "$i is Greater then $host, no need to te +st further. Value of B is $b. Next Host.\n"; next HOST; } else{ print OUT "Host $host passed for $i - this is host + # $b with a default of $defKey\n"; } } } print OUT "# of Hosts: ".$b." Original Mask: ".$defKey." New + Mask: ".$boundary."\n"; print OUT "#################################################\n +"; $trueMaskArray{$defNet} = $boundary; } }

Replies are listed 'Best First'.
Re: Resetting Internal Loop Pointers
by moritz (Cardinal) on Nov 28, 2007 at 19:35 UTC
    Since while only tests a condition, perl has no knowledge of what you consider the "loop pointer".

    You'll have to reset it yourself, of use a for loop (perhaps with a label) instead. Or rethink your code...

Re: Resetting Internal Loop Pointers
by wind (Priest) on Nov 28, 2007 at 20:02 UTC
    Simply change your while to a for loop if each is giving you problems with redo:
    MASK: for my $defNet (keys %defMaskArray) { my $defKey = $defMaskArray{$defNet}; #... redo MASK if $defKey eq 'foobar'; #... }
    - Miller
Re: Resetting Internal Loop Pointers
by jrsimmon (Hermit) on Nov 28, 2007 at 20:24 UTC
    If you want a finer level of control than a redo, you can pull the keys function out of the loop and then iterate over an array with a counter. Then you will have the ability to manipulate the counter however you wish. Here's a simplistic example:
    use strict; my %hash = (test1 => 1, test2 => 2, test3 => 3, test4 => 4); my @array = sort(keys(%hash)); my $counter = 0; my $resetter = undef; my $resets = 0; while($counter <= $#array){ if(defined($resetter)){ $counter = 0; $resets++; } $resetter = undef; print $hash{@array[$counter]}; $counter++; $resetter = 1 if $counter > 2 and $resets < 2; }
    If all you need is the ability to restart the loop, the for/redo suggestion above requires a smaller code change.
Re: Resetting Internal Loop Pointers
by GrandFather (Saint) on Nov 29, 2007 at 00:18 UTC

    You need to reset the each iterator. Using keys does that. Refactoring your code a little may help see what's going on. Consider:

    use strict; while (my ($defNet, $defKey) = each (%defMaskArray)) { ... HOST: for (my $bCount = 1; $bCount <= $hostsN; $bCount++) { ... while (my ($n, $host) = each %$refHandle) { ... my $testResult = test ($bCount, $boundary, $host, $defNet, $de +fKey, \@routeArray); keys %$refHandle if $testResult ne 'ok'; # Reset each redo HOST if $testResult eq 'retry'; next HOST if $testResult eq 'greater'; } ... } } sub test { my ($bCount, $boundary, $host, $defNet, $defKey, $routeArray, ) = +@_; for (my $iCount = 0; $iCount <= 256; $iCount = ($iCount + $boundar +y)) { # evaluate boundaries if ($iCount == $host || (($iCount + $boundary) - 1) == $host) +{ print OUT "$defNet for $host fell on a boundary for mask - $boun +dary\n"; if ($boundary == 256) { ... } else { ... return 'retry'; } } elsif ($iCount > $host) { ... return 'greater'; } else { ... } } return 'ok'; }

    Note too that you should use strictures (use strict; use warnings;) and generally avoid using $a and $b except for their magical use in the context of sort.


    Perl is environmentally friendly - it saves trees