Newbie question: load data to array

Newbie question: load data to array

am 16.09.2007 17:20:30 von philbo30

I have a file with data like this:

123
456
789
010
987
654
321

I want to load these values into an array called "$data". How do I
make this happen?

Re: Newbie question: load data to array

am 16.09.2007 18:02:58 von charley

On Sep 16, 11:20 am, philbo30 wrote:
> I have a file with data like this:
>
> 123
> 456
> 789
> 010
> 987
> 654
> 321
>
> I want to load these values into an array called "$data". How do I
> make this happen?

The manual explains this. See:

perlopentut
perlfunc (look for the function 'push')

Array names in perl are preceded with an '@' character, not '$'.

See: perlintro

Chris

Re: Newbie question: load data to array

am 16.09.2007 21:40:24 von Martijn Lievaart

On Sun, 16 Sep 2007 15:20:30 +0000, philbo30 wrote:

> I have a file with data like this:
>
> 123
> 456
> 789
> 010
> 987
> 654
> 321
>
> I want to load these values into an array called "$data". How do I make
> this happen?

open my $fh, "<", $filename or die "Cannot open $file: $!";
my @data;
while (<$fh>) {
push @data, $_;
}
close $fh;

or use File::Slurp.

HTH,
M4

Re: Newbie question: load data to array

am 17.09.2007 12:40:10 von Ben Morrow

Quoth Martijn Lievaart :
> On Sun, 16 Sep 2007 15:20:30 +0000, philbo30 wrote:
>
> > I have a file with data like this:
> >

> >
> > I want to load these values into an array called "$data". How do I make
> > this happen?
>
> open my $fh, "<", $filename or die "Cannot open $file: $!";
^^^^^^^^^ ^^^^^
One of these is not the same as the other... :)

> my @data;
> while (<$fh>) {
> push @data, $_;
> }

Huh? You're the second person who's suggested doing it like this... it's
really not necessary. Just use <> in list context:

my @data = <$fh>;

> close $fh;

If you're not going to check the return value of close there's no need
(with a lexical FH) to call it. I would generally shorten the whole
thing to

my @data = do {
open my $FH, '<', $filename
or die "cannot read '$filename': $!";
<$FH>;
};

> or use File::Slurp.

....or that, yeah :).

Ben

Re: Newbie question: load data to array

am 18.09.2007 04:11:57 von Tad McClellan

Ben Morrow wrote:
> Quoth Martijn Lievaart :

>> close $fh;
>
> If you're not going to check the return value of close there's no need
> (with a lexical FH) to call it.


There's no need if the FH was opened for input.

If, however, it was opened for output, then an explicit close() is
a pretty good idea, even when you don't check its return value.

If you like to see others in pain, then check out my tale of woe:

http://groups.google.com/group/comp.lang.perl.misc/msg/73d45 87743c64e2f


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Newbie question: load data to array

am 18.09.2007 15:29:04 von Ben Morrow

Quoth Tad McClellan :
> Ben Morrow wrote:
> > Quoth Martijn Lievaart :
>
> >> close $fh;
> >
> > If you're not going to check the return value of close there's no need
> > (with a lexical FH) to call it.
>
> There's no need if the FH was opened for input.
>
> If, however, it was opened for output, then an explicit close() is
> a pretty good idea, even when you don't check its return value.
>
> If you like to see others in pain, then check out my tale of woe:
>
> http://groups.google.com/group/comp.lang.perl.misc/msg/73d45 87743c64e2f

Yes, but that's about using global filehandles, which are only closed
when you re-open them. Lexical filehandles are explicitly closed by perl
at the point where they go out of scope, so don't cause that problem
(as, indeed, Anno pointed out in that thread... :) ).

Ben