Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Reading File Into Array

by dasgar (Priest)
on Mar 28, 2016 at 02:11 UTC ( [id://1158920]=note: print w/replies, xml ) Need Help??


in reply to Reading File Into Array

There are at least a couple of different ways to do this.

Here's one example using Path::Tiny:

use strict; use warnings; use feature 'say'; use Path::Tiny; my $file = 'test_file.txt'; my @lines = path($file)->lines({chomp => 1}); my $line_number = 1; foreach my $line (@lines) { say "$line_number: $line"; $line_number++; }

Here's an example that uses Tie::File:

use strict; use warnings; use feature 'say'; use Tie::File; my $file = 'test_file.txt'; tie @lines, 'Tie::File', $file or die "Unable to open file '$file': $! +"; my $line_number = 1; foreach my $line (@lines) { say "$line_number: $line"; $line_number++; }

Here's an example that does not use any modules:

use strict; use warnings; use feature 'say'; my $file = 'test_file.txt'; open(my $fh, "<", $file) or die "Unable to open file '$file': $!"; my $data; { local $/; undef $/; $data = <$fh>; } close($fh); my @lines = split /\n/,$data; my $line_number = 1; foreach my $line (@lines) { say "$line_number: $line"; $line_number++; }

Replies are listed 'Best First'.
Re^2: Reading File Into Array
by Irishboy24 (Sexton) on Mar 28, 2016 at 02:39 UTC
    Hi, Thanks for the feedback. I actually wanted my output to be like this.
    Cech Bellerin Per Koscielny ..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158920]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-18 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found