Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: how to make first element of an string as a the array name

by radiantmatrix (Parson)
on Nov 15, 2007 at 17:04 UTC ( [id://651021]=note: print w/replies, xml ) Need Help??


in reply to how to make first element of an string as a the array name

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!

<radiant.matrix>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2024-04-18 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found