Perhaps a hash, or an array is would suit your purpose better. Here is an example using an array, also showing a way to put the vals in $i, $j, $k and a way to use a hash.
use strict; use warnings; my @names; while (my $name = <DATA>) { chomp $name; # remove newline push @names, $name; } print "The third name is $names[2]\n"; my @copy = @names; # take a copy for the hash demo my ($i, $j, $k); while (@names) { for (\$i, \$j, \$k) { if (@names) { $$_ = shift @names; } else { $$_ = 'ran out of records'; # could use undef too } } print "i: $i j:$j k: $k \n"; } # and with a hash my %hash; while (@copy) { %hash = (i=>'', j=>'', k=>''); # clear the hash for my $key (qw(i j k)) { $hash{$key} = shift @copy if @copy; } print "the hash contains $hash{$_} under $_\n" for ('i'..'k'); } __DATA__ a b c d e f g h
Cheers,
R.
In reply to Re: Save first n values from a file
by Random_Walk
in thread Save first n values from a file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |