I have two files: /tmp/carlist
DATSUN_BLUE TOYOTA_MAROON ELANTRA-YELLOW
/tmp/location
1250 1 engineer office DATSUN_BLUE office 67 page30 text 1500 1 billing office MERCEDES-TAN office 98 page 40 txt 1500 7 billing office JAGUAR-BLACK office 98 page 40 txt 1250 0 engineer office ELANTRA_YELLOW office 66 page50 txt 1250 1 engineer office DATSUN_BLUE office 67 page30 text
The second file has 100s of lines. I'd like to search for the patterns in the first file in the second file and save them into separate files. So the lines that have DATSUN_BLUE and the fields criteria would go to /tmp/Datsun.txt and the ones that contain TOYOTA_MAROON and criteria would go to /tmp/Toyota.txt, etc. I want the files to be created per line found in first file. So instead of one big file with the patterns, each time a pattern is found it would go to a separate file. I have tried, and I can create an $fh3 output file, but can't create separate files for each pattern in the first file. Does anyone have an idea?
open my $fh2, '<', '/tmp/location' or die "unable to open file for rea +ding : $!"; open my $fh1, '<', '/tmp/carlist' or die "unable to open file for rea +ding : $!"; chomp(my @carlist = <$fh1>); print "these are the policies @carlist\n"; close $fh1; my $pattern = join '', @carlist; my @strings; while (<$fh2>) { my @fields = split(',', $_); local $" = ','; if ( $fields[1] eq '1' || $fields[1] eq '0') { push @strings, $_ if /$pattern/; print $fh3 "@fields[0,4]\n" if /engineer/ && !/,-,/; } } close $fh2; close $fh1; close $fh3;

In reply to how to save patterns found in array to separate files by 2015_newbie

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.