Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Save first n values from a file

by Random_Walk (Prior)
on Jan 10, 2014 at 10:33 UTC ( [id://1070090]=note: print w/replies, xml ) Need Help??


in reply to Save first n values from a file

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.

Update: Added hash demo to code too, and better behaviour when it runs out of values

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.

Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found