Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: I am a beginner for perl

by cavac (Parson)
on Oct 11, 2011 at 12:08 UTC ( [id://930795]=note: print w/replies, xml ) Need Help??


in reply to I am a beginner for perl

First of all, the hashbang line (first line) shouldn't contain a space between the exclamation mark and the slash, so instead of
#! /usr/bin/perl
it should be
#!/usr/bin/perl

Second, i have the feeling that your code is overly complicated. If you loop $i 10 times and only use the first five times, why loop the 10 times anyway? do/until isn't that readable, if or while could do a better job. The foo: tag and next jump also don't help to clarify the code.

There also seems to be another logical flaw with foo/next/do/until: I'm just not sure what result you expect: Just increment $j until it's greater than 10?.

Ok, given above assumptions, let's try to write an easier version, shall we?

#!/usr/bin/perl use strict; use warnings; my $i = 0; my $j = 1; while($i < 10) { if($i < 5) { $j += 2; } if($j > 10) { last; } } print "Result: $j\n";


If my assumptions are correct so far, we could just remove variable $i altogether and just increment $j until the result is reached:
#!/usr/bin/perl use strict; use warnings; my $j = 1; while($j <= 10) { $j += 2; } print "Result: $j\n";


Of course, in this simple example, we could also provide a formula to do the same. In this case, instead of hardcoded numbers 10 and 2 i'll use $minimum and $stepsize:
#!/usr/bin/perl use strict; use warnings; my $j = 1; my $minimum = 10; my $stepsize = 2; my $steps = int($minimum/$stepsize); # If startpoint is zero, we need to add a step if($j == 0) { $steps++; } $j = ($steps * $stepsize) + $j; print "Result: $j\n";


While the last solution looks more complicated, it scales better. Think is you don't have 10 steps but 10 billion. The formula would still take the same time while the loop constructs will run for a long time.

Hope that helped to clarify a bit and i didn't misunderstand your problem.
Don't use '#ff0000':
use Acme::AutoColor; my $redcolor = RED();
All colors subject to change without notice.

Replies are listed 'Best First'.
Re^2: I am a beginner for perl
by choroba (Cardinal) on Oct 11, 2011 at 12:29 UTC
    hashbang line (first line) shouldn't contain a space between the exclamation mark and the slash
    Not true. Space is allowed.
      Well, yes, it is allowed. Though i'm pretty sure not all programs parsing hashbangs know that.

      But thanks for clarifying that. I should have written it that way in the first place...
      Don't use '#ff0000':
      use Acme::AutoColor; my $redcolor = RED();
      All colors subject to change without notice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://930795]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found