from text file to array in shortest way

B

Brian Wakem

ngoc said:
my @books_array = ();
open(FILE, '<books_file.txt') or croak 'Can open file';
while (<FILE>){
push @books_array, $_;
}
Other shorter ways than above?

open......
my @books_array = <FILE>;
 
J

John Bokma

ngoc said:
my @books_array = ();

= () is not needed.
open(FILE, '<books_file.txt') or croak 'Can open file';
while (<FILE>){
push @books_array, $_;
}
Other shorter ways than above?

use File::Slurp;

my @books_array = read_file( 'filename' );
 
N

ngoc

my @books_array = ();
open(FILE, '<books_file.txt') or croak 'Can open file';
while (<FILE>){
push @books_array, $_;
}
Other shorter ways than above?
 
V

Veli-Pekka Tätilä

ngoc said:
my @books_array = ();
open(FILE, '<books_file.txt') or croak 'Can open file';
while (<FILE>){
push @books_array, $_;
}
Other shorter ways than above?

Hi,
I'm not sure if this qualifies as an entry but one way would be to use the
diamond operator and specify a file name on the command-line. The down-side
with this approach is that you cannot, to my knowledge, change the error
message displayed by Perl. If the file canot be opened, you're program will
not even run because the file is thought of as STDIN and Perl fails to get
it. Or this is how i think of it from the user point of view.

Also, I think croak is not used by default so error messages may be reported
differently, that is the exact line where it died rather than giving the
callers line number.

The code would be:

my @books_array = <>;

If you want a custom error message, you'll have to open the file explicitli
I guess. Or maybe a die-handler can also do the croaking, though that's at
least one extra line and a bit of a hack, too. The thing I 've found
confusing is that die-handlers can get called even inside an eval block,
where a death is not a fatal thing (the program can still continue).

I wonder how we measure shortness in this particular case? A one-line loop
like:

push @books_array, $_ while <FILE>;

does exactly the same thing as your while loop but saves a few braces. But
then again, this is not good for the future. If you'll need to do more than
one thing inside the while loop, you can either try tricks with the comma
operator or give up and make it a more regular while. Similarly, if
strictness is not required you could just use the array without mentioning
it first.

I'm rather new to Perl still but thought I'd give this a try.
 
E

Eric J. Roode

ngoc said:
my @books_array = ();
open(FILE, '<books_file.txt') or croak 'Can open file';
while (<FILE>){
push @books_array, $_;
}
Other shorter ways than above?

use Tie::File;
tie my @books_array, 'books_file.txt' or die "Can't open: $!";

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top