于 2011-3-17 17:23, wisma laili 写道:
I have problem in reading a file and change it to get some specific  output. For
example : I want to read a file : filename.txt which contain  2 lines:
Data A 1 2 3 4 5
Data B 6 7 8 9 10
the name of the data and the values are tab separated.  my wish is to change it
into :Data A = (1,2,3,4,5); Data B = (6,7,8,9,10);

Thanks in advanced for your kindly help!


So, split each line and get a list, then create a hash with the first two elements in the list as key and the left elements as value?

use strict;

my %hash;

while(<DATA>) {
    chomp;
    my @list = split;
    my $name = join " ",(shift @list,shift @list);
    $hash{$name} = [@list];
}


__DATA__
Data A 1 2 3 4 5
Data B 6 7 8 9 10

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to