Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^3: New student, can we write this program in perl...

by choroba (Bishop)
on Oct 19, 2012 at 17:15 UTC ( #999986=note: print w/replies, xml ) Need Help??

in reply to Re^2: New student, can we write this program in perl...
in thread New student, can we write this program in perl...

I would change your program to this one:
#!/usr/bin/perl use warnings; use strict; print 'Enter the number of elements: '; my $n = <>; # Read the input. chomp $n; # Remove the newline. print "Enter $n numbers.\n"; my @a; for (1 .. $n) { push @a, scalar <>; # Push numbers to the +array. } chomp @a; # Remove the newlines. print join (' ', 'The even numbers are:', grep $_ % 2, @a), # grep filters the num +bers = 1 modulo 2 "\n"; print join (' ', 'The odd numbers are:', grep 1 - $_ % 2, @a), "\n";
Not really similar, is it? The main differences:
  • No main function needed.
  • Variables are declared where needed.
  • The diamond operator is used to read from the input.
  • for can be used with simpler syntax, avoiding off by 1 errors (see perlsyn)
  • grep can be used to filter arrays. No need for temporary variables to keep the results if you do not need them later.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999986]
[LanX]: YM: talking about breaches ... did you read about Cambridge Analytica harvesting 50e6 FB profiles for political propaganda?
LanX ... oh ... Brexit delayed ... again ... oh wonder ...
[Your Mother]: Heh. I keeping seeing related headlines but my narcissism is a perihelion right now so I'm busy. :P
[LanX]: https://www. news/series/ cambridge- analytica-files
[Your Mother]: s/a /at /;

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2018-03-19 13:17 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.