Hello 2015_newbie, and welcome to the Monastery!

Since Perl v5.6, you can use lexical filehandles, which behave like other scalar variables. In this case, store the filehandles in a hash with each filehandle keyed to the corresponding filename base. The following code should give you the idea:

#! perl use strict; use warnings; my @carslist = qw(DATSUN_BLUE TOYOTA_MAROON ELANTRA_YELLOW); my %cars; for (@carslist) { unless (exists $cars{$_}) { my $file = 'tmp/' . $_ . '.txt'; open(my $fh, '>', $file) or die "Cannot open file '$file' for writing: $!"; $cars{$_} = $fh; } } my $pattern = join '|', @carslist; $pattern = qr/$pattern/; my @strings; while (<DATA>) { chomp; my @fields = split; if (($fields[1] eq '1' || $fields[1] eq '0') && /$pattern/) { push @strings, $_; print { $cars{$fields[4]} } $fields[0], ',', $fields[4], "\n" if /engineer/ && !/,-,/; } } for (@carslist) { close $cars{$_} or die "Cannot close file '$_': $!"; } print "Strings:\n"; print "$_\n" for @strings; __DATA__ 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

Output:

16:14 >perl 1499_SoPW.pl Strings: 1250 1 engineer office DATSUN_BLUE office 67 page30 text 1250 0 engineer office ELANTRA_YELLOW office 66 page50 txt 1250 1 engineer office DATSUN_BLUE office 67 page30 text 16:14 >

and the files DATSUN_BLUE.txt, TOYOTA_MAROON.txt, and ELANTRA_YELLOW.txt created and populated as required.

Updates: (1) Note also that your script splits on commas, but the data you show is separated by whitespace. I’ve changed the split statement to just my @fields = split;, which is special-cased to split the default variable, $_, on whitespace (as though the pattern were /\s+/).

(2) The braces are required around the filehandle expression in the line:

print { $cars{$fields[4]} } $fields[0], ',', $fields[4], "\n" if /engi +neer/ && !/,-,/; # ^ ^

because “whenever you’re using any expression more complex than a bareword handle or a plain, unsubscripted scalar variable ... you will have to use a block returning the filehandle value...” (perldoc print).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: how to save patterns found in array to separate files by Athanasius
in thread 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.