开发者

looping in perl

开发者 https://www.devze.com 2023-03-25 04:28 出处:网络
I am writing this loop where in the initializing i am intializing using a variable and not a absolute value.Why doesn\'t the value change? Or is it not allowed in the for loop?

I am writing this loop where in the initializing i am intializing using a variable and not a absolute value.Why doesn't the value change? Or is it not allowed in the for loop?

enter code here
 for($i = $one, $i > $top_level,$i--)
    {  
      print $i,"\n";
      print "One is:",$one,"\n";
     }

Why can't开发者_开发技巧 i assign $i= $one. The variable $one keeps changing so thats the reason why i declared $i to $one.When i print the individual values of the variables the values are correct, its just that in the for loop the value is not being assigned.


Use semicolons, not commas.

for($i = $one; $i > $top_level; $i--)
{  
    print $i,"\n";
    print "One is:",$one,"\n";
}

Avoid C-Style loops in perl, if possible.

for my $i (reverse $top_level..$one) {
    print $i,"\n";
    print "One is:",$one,"\n";
}


The separator inside the for() statement is ";", not ",".


You've accidentally stumbled on using for with a list, by using commas instead of semi-colons. Watch what happens when you add a third print statement:

    print "\$_=[$_]\n";

with $one as 1, I get:

$_=[0]
...
$_=[]
...
$_=[1]

And that is because there are three items in the list. First of all what you wanted to do, would have looked like this:

for my $i ( $one..( $top_level - 1 )) {

In this loop, $i is set to each member of the range and then the loop is executed. If we don't suppy the variable, perl assigns it to $_.

for ( $one..( $top_level - 1 )) {

And perl does not wait to compile the list, so before it ever execute the first time it goes through all the terms.

$i = $one; 
# push actual $i returned by expression $i = $one
push @a, $i;                  
# result = [ $i=1 ]
# push boolean false => ''
push @a, ( $i > $top_level ); 
# result = [ $i=1, '' ]
# push the return of $i--, NOT $i
push @a, ( $i );              
# result = [ $i=1, '', 1 ]
# decrement $i
$i--;                         
# result = [ $i=0, '', 1 ]

You can find this out using a TIE-ed scalar:

package Monitored;
sub TIESCALAR {
    my ( $class, $name, $value ) = @_;
    return bless { name => $name, value => $value }, $class;
}

sub FETCH {
    my $self = shift;
    Carp::carp "Reading \$$self->{name}...";
    return $self->{value};
}

sub STORE { 
    my $self = shift;
    my $value = shift;
    Carp::carp "Storing \$$self->{name}=${\(defined( $value ) ? $value : 'undef')} ";
    $self->{value} = $value;
}

And this initialization in the mainline:

my $one       = 1;
tie my $i,         'Monitored', 'i';
tie my $top_level, 'Monitored', 'top_level', 5;
for($i = $one, $i > $top_level,$i--) # line 30
    {  
      print "*LOOP*\n";
      #print "\$i=$i\n"; <-- commented out to reduce noise
      #print "\$one=$one\n";
      print "\$_=[$_]\n";  # line 35
     }

Then in running the loop the output is:

Storing $i=1 at - line 30
Reading $top_level... at - line 30
Reading $i... at - line 30
Reading $i... at - line 30
Reading $i... at - line 30
Storing $i=0  at - line 30
*LOOP*
Reading $i... at - line 35
$_=[0]
*LOOP*
$_=[]
*LOOP*
$_=[1]

Note that only one time, at line 35, do we access $i after the looping starts.


What on earth are you trying to do? Is this a normal 'for' loop or are you trying to do something exotic. Note use of semicolon, NOT comma. Using a comma in any loop does something completely different: runs each bit of code each time.

Normally it would be:

$one = 1;
for($i = $one; $i > $top_level;$i--)
{  
  print $i,"\n";
  print "One is:",$one,"\n";
 }

It is Perl right?

0

精彩评论

暂无评论...
验证码 换一张
取 消