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

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

I want to compare what is in $ARGV[0] an array that I created from a file that contains single line entries.

This is what I have so far:
#!c:\perl\bin -w use strict; open downfile, '>f:\scripts\text\nodedown.txt': print downfile "$ARGV[1] $ARGV[2] is Down on $ARGV[0].\n"; close downfile; my $routers="F:/scripts/text/routers.txt"; open routers, $routers or die "cannot open $routers: $!\n"; my @lines = <routers>; close routers; my ($zero_arg, $first_arg, $sec_arg) = @ARGV[0..2]; if (grep {$zero_arg eq $ARGV[0]} @lines) { system('f:\usr\ov\bin\nvecho.exe GOT IT'); } exit 0;
Is there a better way to do this?

Replies are listed 'Best First'.
Re: compare what is in ARGV0 to a file
by Fastolfe (Vicar) on Nov 01, 2000 at 02:22 UTC
    Be aware that each element in @lines will have the trailing newline from the file. You probably want to chomp(@lines); before you compare it against anything.

    In addition, your grep statement is non-sensical. See the documentation for it. If you want to loop through @lines and compare it against $ARGV[0] (why not $zero_arg?), you'll have to either explicitely set up a loop to do it, or modify your grep statement to compare against $_, which will iteratively contain each element in @lines in turn.

      To amplify Fastolfe's point ... your grep grabs everything in the array, for the simple reason that the line immediately preceding it sets $zero_arg equal to $ARGV[0] (you thus get every line which is such that the first argument passed to the script is the same as the first argument passed to the script). If your first argument is the thing you're looking for in the file, this is obviously no good. use $_ as Fastolfe suggests, that's the "default variable".

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: compare what is in ARGV0 to a file
by Ovid (Cardinal) on Nov 01, 2000 at 02:22 UTC
    dallok: this appears to be at least the third time that you have asked a variation of this question. I think you would be better served posting this to the Seekers of Perl Wisdom section. Then, rather than post it three times (with little to no explanation), you could generate a running commentary where people ask "What are you talking about?" and you get a chance to clarify it, rather than repeatedly posting "non-questions" like the one above.