Hello again, 2015_newbie,

A typical print statement has the following syntax:

print FILEHANDLE LIST

where LIST is a list of what things to print, and FILEHANDLE is a special value telling Perl where those things are to be printed. If FILEHANDLE is omitted, the destination defaults to the last selected filehandle — typically STDOUT, which means print to the screen. If you want your output to go to a file — as you do here — then you need to open the file for writing and associate that opened file with a filehandle variable; then you give that filehandle to print to tell it where to print to.

Now, let’s return to the line:

print { $cars{$fields[4]} } $fields[0], ',', $fields[4], "\n" if /engineer/ && !/,-,/;

It might make things clearer if we add a couple of variables within the if clause:

if (($fields[1] eq '1' || $fields[1] eq '0') && /$pattern/) { push @strings, $_; my $number = $fields[0]; my $model = $fields[4]; print { $cars{$model} } $number, ',', $model, "\n" if /engineer/ && !/,-,/; }

Now it’s easier to see: in the LIST, the variable $model is what gets printed (second); but in the expression which denotes the filehandle, $model is the key corresponding to the filehandle stored in the hash %cars. So the value of $model (i.e., $fields[4]) might be DATSUN_BLUE (a string), but the corresponding hash entry, $cars{$model} (i.e., $cars{$fields[4]}), will be a special value — one which Perl understands — which identifies the file “tmp/DATSUN_BLUE.txt” as the file for print to write to.

It might help you to study perldata to get a good grasp of arrays and hashes.

Update: Corrected typo and improved wording.

Hope that helps,

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


In reply to Re^3: 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.