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.


In reply to Re: grep exact words by andyf
in thread grep exact words by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.