'N'other way of acquiring the contents of the files ... but may not be what you want?

#! /usr/bin/perl -w use 5.018; #1123337 my @arr; my @files=("Die2.txt", "Die1.txt", "Die1.bak", ); # assumes that you have code to ID the fil +es # you want and collect them into an array for (@files) { open (my $InFile, '<', $_) or die "cannot open $_, $!"; while (<$InFile>) { my $var = $_; push @arr, $var; } } say @arr;

UPDATED: with the notes below.

And a note: If the way you phrased "... how I can get my already working script's array to read the contents of different files" is merely because English is not your native language it's not a big deal because I see I'm not the only Monk to decipher it (well, I hope we've done so accurately).

But if that phrasing accurately reflects your understanding of arrays, note that neither that array or any other can "read the contents" of anything. You have to explicitly tell Perl that you want data PUSHed onto an array... and that means you have to collect the data first -- by the method shown here, or by that in the prior ( ++ ) reply.

You could, of course, read about the record-input separator ($/, the $INPUT_RECORD_SEPARATOR ( aka "the field record separator" at Perl.com )and use that instead of the while loop above; which would spare you the need for Ln 19. Setting $/ = undef; will cause your read to slurp the entire file.


In reply to Re: How do I get different files into an array by ww
in thread How do I get different files into an array by amagana

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.