I know they are scary at first but this is the kind of thing regular expressions were born for.

The sample code below contains some comments which may help.
use warnings; my @array = qw (A B C D); ## This example uses the open-file-handle-as-string feature ## Substitute \(my ...) for filepath open (my $INFILE,"<", \(my $in = join "\n", qw(aA d DDD CDA e XYZ AABB +CC))); open (my $OUTFILE,">", \(my $out)); ## Create a regular expression for all elements in the array. ## With the contents of array above the string created will be ## A+|B+|C+|D+ ## Note how the '+' is suffixed on at the end because the 'join' inser +ts ## between array elements. my $composite_elements_re = join ('+|',@array) . '+'; ## Turn the string into a real regex with qr. ## The 'x' modifier means you can add comments between the '/'s ## See perlretut etc in the perl documentation my $complete_re = qr/ $composite_elements_re /x ; while (my $rec = <$INFILE>){ ## The g modifier means get all matches. my @matches = $rec =~ /($complete_re)/gx ; ## If there are any matches print them to the output string (via f +ilehandle!) ## Note that '@matches and' forces @matches to be interpreted as a + scalar (single) value. ## An array treated as a scalar returns the number of elements. ## So long as @matches >= 1 the matches will be put in the output. @matches and do { print $OUTFILE @matches }; } close ($INFILE); close ($OUTFILE); ## Change to print to file of your choice - or leave as stdout? print "$out\n"; ## prints ADDDCDAAABBCC

In reply to Re: Using array elements for a file search by mrstlee
in thread Using array elements for a file search by Jeri

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.