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

I have this part of my script that works but I now need it to take an arbitrary number of numbers on the command line and print all the even numbers. How would I do this with the shift or $ARGV?
my $webpage = $ARGV[0] || die "No Web page number given.\n"; if (int($webpage/2) == ($webpage/2)) { print "$webpage\n";#JUST WANT EVEN PAGE NUMBERS }

Replies are listed 'Best First'.
Re: Adding command line arguments.
by Joost (Canon) on Jun 06, 2002 at 14:04 UTC
    print grep {$_ % 2 == 0} @ARGV;
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
      maybe
      print join("\n", grep {$_ % 2 == 0} @ARGV), "\n";
      if it needs to be human readable :)
      Thanks for all answers, you guys are quick!
Re: Adding command line arguments.
by stajich (Chaplain) on Jun 06, 2002 at 14:08 UTC
    Not sure I understand the question completely but the following will print the integers in an array which are even.
    for my $w ( @ARGV) { next if( $w !~ /^\d+$/); # skip non-numerics if( ($w % 2) == 0 ) { print "$w\n" } }
Re: Adding command line arguments.
by marvell (Pilgrim) on Jun 06, 2002 at 14:13 UTC

    How about ...

    my @pages = grep {$_ % 2 == 0} @ARGV;

    Or are you asking something else?

    --
    Steve Marvell

Re: Adding command line arguments.
by robobunny (Friar) on Jun 06, 2002 at 14:06 UTC
    how about:
    foreach $number (@ARGV) { print "$number\n" unless ($number % 2); }
Re: Adding command line arguments.
by u914 (Pilgrim) on Jun 06, 2002 at 14:55 UTC
    i'll stab at this.... mind you, i'm on a locked down terminal and can't test this...
    foreach(@ARGV) { if (int($_/2) == ($_/2)) { print "$_\n"); } }
    though, i could be totally wrong...

    Also, look into the possibility that Perl has a "mod" operator.. i don't know offhand, but most other languages do.

    it works like $remainder = mod($number, $divisor), wherein $remainder ends up with the leftover (remainder) from $number/$divisor.

    so, if you had mod(6, 2) the remainder would be 0, but for mod(6, 4) the remainder would be 2

    i'm not all that familiar with Perl, but there must be some similar functionality....

    Update

    argh! that's what i get for replying while i'm at work, and get called away.... in the hour between starting|finishing my post, lots of others have (better) answered...

    anyhow, good luck!

Re: Adding command line arguments.
by ariels (Curate) on Jun 06, 2002 at 14:41 UTC
    Easy. Dunno why it matters how many command-line arguments there are. But hey! -- We all have to make some effort...
    #!/usr/local/bin/perl use strict; use warnings; use integer; my $even = -1<<31; do { print "$even\n"; $even += 2; } while ($even != -1<<31);

    NOTES

    1. Untested code.
    2. You might run out of paper if you print the output.
    3. Only works for 32-bit 2's complement machines.
    4. Even there, it doesn't really print all the even numbers.
    5. 1's complement machines (does Perl run on any?) will print odd numbers too
    6. Doesn't test the generated numbers for evenness.