in reply to grep exact words

Here's another take. It looks like you want to read an argument list for a program. It also looks like you are just using the file as a temporary test method to develop the prog, as I have followed in the example below. But you are possibly making life too complicated for yourself using a list to store what will in reality be a string of values or flags. When you come to use this code for real, to get arguments replace <FILE> with @ARGV at line 7. Alternatively, try using getopt::Std.

#!/usr/bin/perl my $list = './argslist'; # test file of args my @argstomatch = ('--hack','--and','--pray','--elephants'); my $srvr; open (FILE, "<$list") or die "open $list failed: $!\n"; for(<FILE>){$srvr .= $_}; # append all lines together close (FILE); # at this point we are done collecting - everything is + in one $srvr string # the rest demonstrates index for $argfindme(@argstomatch) { printf ("%s\t%s\n", $argfindme, index($srvr,$argfindme)); }

You will see a number next to each argument in the @argstomatch list. This is -1 if the argument doesn't exist, otherwise it's the position of the first character of the match in $srvr.

Replies are listed 'Best First'.
Re^2: grep exact words
by Anonymous Monk on Jun 27, 2004 at 01:48 UTC
    Hello noble people, Thank you kindly for all your answers....but since I am short of time...I have found an alternative method to get around this... for ($i = 0; $i <= $#ARGV; $i++) { $gname = `grep -w @ARGV$i $server_list`; if ( length($gname) == 0) { print "ERROR: Bad input to the script\n"; } This seems to work for now.