Hello WisDomSeeKer34,

Although the fellow Monks have replied to your question, I would like to add something extra.

On your question you are asking on how to process one file, and the monks have provided you the answer. I would like to add here that while loop is faster than straight opening a file in an array but also to add using a hash to keep track of the content of multiple files.

Sample of benchmark script and hash solution commented:

#!usr/bin/perl use strict; use warnings; use Data::Dumper; # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # LinuxOS my @preserved = @ARGV; =Hash Of Arrays my %HoA; while (<>) { chomp; push @{ $HoA{$ARGV} }, $_; } continue { close ARGV if eof; } print Dumper \%HoA; __END__ $ perl test.pl in.txt out.txt $VAR1 = { 'in.txt' => [ 'Sample of text line 1', '', 'Sample of text line 2' ], 'out.txt' => [ 'Sample of text line 1 second file', '', 'Sample of text line 2 second file' ] }; =cut sub while_loop { my @output; @ARGV = @preserved; # restore original @ARGV while (<>) { chomp; push @output, $_; } continue { close ARGV if eof; } return; } sub file_to_array { @ARGV = @preserved; # restore original @ARGV chomp (my @data = <>); close ARGV if eof; return; } my $results = timethese(10000000, { Array => \&file_to_array, While => \&while_loop }, 'none'); cmpthese( $results ); __END__ $ perl test.pl in.txt in2.txt Rate Array While Array 115513/s -- -4% While 120846/s 5% --

Maybe you are not interested in opening multiple files (today) but what about (tomorrow)?

Update: One minor thing also to add here. Always always remember to close your file handles. Why? read this: Why do we need to close filehandles?.

Update2: Minor thing updating @ARGV to $ARGV in Hash Of Arrays (HoA) solution.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: from txt file to array by thanos1983
in thread from txt file to array by WisDomSeeKer34

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.