http://www.perlmonks.org?node_id=586888

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

dear monks

Prog_name : print.pl #!/usr/bin/perl use strict; use warnings; my @array = ( 11,12,13,14,15); #my @new = @array[join(",",@ARGV)]; my @new = @array[1,3]; print "@new\n"; Output :- 12 14
Changed the code as follows :-
#!/usr/bin/perl use strict; use warnings; my @array = ( 11,12,13,14,15); my @new = @array[join(",",@ARGV)]; #my @new = @array[1,3]; print "@new\n"; Output : perl print.pl 1 3 Argument "" isn't numeric in array slice at l.pl line 8. 11
Is it mandatory that all the arguements which I am passing from command line arguements should need to be converted to numbers ( if it is a number ) to print ? Is there anyother way in which, will I be able to tell my program to treat the command line arguements as integers ?

Replies are listed 'Best First'.
Re: print the array using command line arguements
by Sidhekin (Priest) on Nov 30, 2006 at 07:48 UTC

    my @new = @array[join(",",@ARGV)];

    You're building a string (using join) and then using that (one) string as array indices. That is certainly not what you want.

    I can only imagine you want to use your arguments themselves as array indices, in which case it's straight forward:

    my @new = @array[@ARGV];

    You should be getting Argument "1,3" isn't numeric in array slice and an output of 12 though, so unless you've been mixing up different runs, I'm beginning to suspect your arguments 1 3 aren't being passed to the program.

    Which would be another problem.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: print the array using command line arguements
by loris (Hermit) on Nov 30, 2006 at 07:57 UTC

    Hi,

    Try:

    my @new = @array[@ARGV];

    The problem with your join is that you get a string containing commas, which cannot be treated as an integer. It will only work if you give just one index on the command line, since the join will not then introduce any commas.

    HTH,

    loris


    "It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ."
Re: print the array using command line arguements
by ocs (Monk) on Nov 30, 2006 at 08:26 UTC
    Hi, you can also try this:
    #!/usr/bin/perl -wl use strict; my @array = (11, 12, 13, 14, 15); my @new = map { $array[$_] } @ARGV; print "@new";
Re: print the array using command line arguements
by Fengor (Pilgrim) on Nov 30, 2006 at 07:48 UTC
    The problem in your code is the line my @new = @array[join(",",@ARGV)]; because the first element of @ARGV is the program name Fengor writes a 100 times ksh is not perl try
    #!/usr/bin/perl use strict; use warnings; my @array = ( 11,12,13,14,15); #my @new = @array[join(",",@ARGV)]; #my @new = @array[1,3]; my @new = @array[$ARGV[0],$ARGV[1]]; print "@new\n";

    --
    "WHAT CAN THE HARVEST HOPE FOR IF NOT THE CARE OF THE REAPER MAN"
    -- Terry Pratchett, "Reaper Man"

      The first argument of @ARGV is not the progname
      perl -wle 'print "@ARGV"' 3 4 The progname is in $0
        oh right.

        --
        "WHAT CAN THE HARVEST HOPE FOR IF NOT THE CARE OF THE REAPER MAN"
        -- Terry Pratchett, "Reaper Man"

      nope

      #!/usr/bin/perl use strict; use warnings; print "@ARGV\n";


      Output:
      ~/entwicklung 14> perl argv.pl 1 1 (reneeb) Thu Nov 30 09:05:18 [-bash] ~/entwicklung 15> perl argv.pl 1 3 1 3 (reneeb) Thu Nov 30 09:06:06 [-bash] ~/entwicklung 16> ./argv.pl 1 1 (reneeb) Thu Nov 30 09:06:10 [-bash] ~/entwicklung 17> ./argv.pl 1 3 1 3
      Edit: This is an answer to Fengor, not lidden