Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: grep exact words

by andyf (Pilgrim)
on Jun 26, 2004 at 19:14 UTC ( [id://369860]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://369860]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-16 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found