Hi ,

How do I write a code that will print the first 10 even numbers in PERL

Thank you

Hi ,

How do I write a code that will print the first 10 even numbers in PERL

Thank you

Here is one way:

#!/usr/bin/perl
use strict;
use warnings;
map {print $_ * 2 . "\n"} (1..10);

Here is one way:

#!/usr/bin/perl
use strict;
use warnings;
map {print $_ * 2 . "\n"} (1..10);

wow! it works thank you so much, I have been struggling with this for almost 2 weeks now. how did you know this. I'm new to perl.

Thanks again.

You're welcome. To understand how that works you need to know the shortcut for building a range of numbers, (lowest number followed by two dots followed by largest number, all within parentheses) which results in an array of sequential numbers, in increments of one. Plus you need to know the map function, which performs whatever function or statement you specify on each member of the array or list you specify.

There are other ways of doing the same thing, such as using some kind of loop.

my $x;
for ($x=1;$x<21;$x++){
 print "$x\n" if(int($x/2)==$x/2);
}

Nice one, mitchems. Another way would be

#!/usr/bin/perl
use strict;
use warnings;

my @evens;
my $n = 0;
my $number_to_give_up_at = 100;
# $#evens will contain value of index of last element in @evens array
while ($n++ < $number_to_give_up_at and $#evens < 9){
    push @evens, $n if $n % 2 == 0;
}
print join("\n", @evens);

Spakes, please mark it solved so we can stop working on this.

Well, David, I loved your first solution. I just put that one in there because I have been away for a while and because you mentioned a loop solution. I have done the int thing many, many times to print every other record (or to format it or whatever). Using mod works as well.

It's good to show one of the loop solutions too, as you did, since the OP said he was new to Perl. When I started with Perl, solutions using the map function seemed hard to read. This was the first time I found a good use for map so of course I wanted to show it off.:)

But using a loop can be easier to understand. Here's a minor variation on the loop approach.

#!/usr/bin/perl
use strict;
use warnings;

for (my $n = 2; $n < 21; $n += 2){
    print "$n\n";
}

That solution is slightly more efficient than mine, although I'm not sure it matters much in such a fast-completing script :)

That solution is slightly more efficient than mine, although I'm not sure it matters much in such a fast-completing script :)

Yes, I agree, if the OP is using this in a larger project, printing ten even numbers is not going to be one of the bottlenecks.

I just skimmed through an article that has a gripping title -- Optimization: Your Worst Enemy -- explaining why programmers should optimise only when necessary to solve performance problems.

Excellent article. It might be a little dense for some readers, but it's worth a look.

My view of the matter is pretty simple: in most cases, you'll lose more time by confusing the maintenance coder than you will gain by writing an obscure piece of code, even if it doubles the speed of execution. This is because coding time is measured in hours, and execution time is measured in fractions of a second.

Sometimes it will matter that you save a few instructions - if you're running a graphics-intensive GUI in Java, you really do want to be careful with your cycles, because it makes a difference in how the code runs in the end. However, in most cases, your priorities should be to write clearly, to make your code easily adaptable to reasonable changes in scope, and let optimizing come last.

commented: Well put. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.