I don't think you really want to have the first element be a real array name -- do you have a package named IL12::1 ? If you want to reference these things by name, I suggest (as others have) a hash, with the first element of each row in your file being the key.
Text::CSV_XS would work wonderfully for this application.
#!/usr/bin/perl use strict; use warnings; use IO::File; use Text::CSV_XS; use Data::Dumper; my $io = IO::File->new('myinputfile.txt','<') or die "Cannot open input file for reading: $!"; my $csv = Text::CSV_XS->new(); my %data; while (my $row = $csv->getline($io)) { # getline() reads a line from $io and parses it -- fast! # $row will contain an ARRAYref, one value for each element my $element_name = shift @$row; #first element is the name $data{$element_name} = $row; } print Dumper(\%data); # this will show you how the structure looks
The output from this should look like:
$VAR1 = { 'IL12::1::287' => [ '6', '-17', '-9', '-21', '-24', '-15', '-2', '11', '4', '4', '-15', '-26', '-16', '-9', '-18', '-25', '27', '17', '6' ] ... };
(I've used ... to indicate that there will be more of the same, to save space in this node)
I hope that helps!
In reply to Re: how to make first element of an string as a the array name
by radiantmatrix
in thread how to make first element of an string as a the array name
by koleti
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |