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

Re: How to save first two columns of an array into another array

by vagabonding electron (Chaplain)
on Oct 03, 2012 at 11:06 UTC ( #997030=note: print w/replies, xml ) Need Help??


in reply to How to save first two columns of an array into another array

Since your data file seems to be a text file (like a csv only with whitespace as separator) you could consider using Text::CSV_XS:
#!/usr/bin/perl use strict; use warnings; use Text::CSV_XS; use Data::Dumper; my $param = { binary => 1, auto_diag => 1, sep_char => ' ', eol => $/, }; my %hash; my $csv = Text::CSV_XS->new ( $param ); while ( my $row = $csv->getline( *DATA ) ) { print "@$row[0,1]\n"; # or whatever. $hash{$row->[0]} = $row->[1]; # read into a hash. } print Dumper \%hash; __END__ b c a a c d d e b
Output of a Data::Dumper print statement is:
$VAR1 = { 'a' => 'c', 'b' => 'c', 'd' => 'e' };

Update: Typo corrected.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997030]
help
Chatterbox?
[marto]: good morning all
[Corion]: Hi marto ;)
[Corion]: I hope you're well ;)
[marto]: running late today, get to work and people are just sitting around. "can't delete file, quota exceeded"
[Corion]: marto: Oy :-/
[Corion]: Ah, it's only "quota exceeded", not "filesystem full", so it's "merely" an administrative act to up the FS quota resp. inode quota
[Corion]: I've been on databases where I couldn't drop a table or delete rows because the DB was full. Not so much fun...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2017-01-24 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (203 votes). Check out past polls.