Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Re: another 'array to hash' question

by allolex (Curate)
on Dec 10, 2003 at 15:15 UTC ( [id://313738]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: another 'array to hash' question
in thread another 'array to hash' question

I really don't see why you shouldn't do it the way you have it coded here. And since you probably already know what the fields are, you can even skip the hash altogether. An array of arrays looks good to me.

my @a; while (<INFILE>) { my @line = split /\t/; push @a, \@line; # Array of array references }

Or even:

my @a; push @a, ( split /\t/ ) while (<INFILE>); # array of arrays

which provides a nice list context to put your input directly into an array. :)

--
Allolex

Replies are listed 'Best First'.
Re: Re: Re: Re: another 'array to hash' question
by punkish (Priest) on Dec 10, 2003 at 15:38 UTC
    >And since you probably already know what the fields are,
    >you can even skip the hash altogether. An array of arrays
    >looks good to me

    unfortunately, I need an array of hashes ;-). The best use is to just stick a ref to that array inside a tmpl_loop in HTML::Template. Besides, an aOfH is more intuitive for me. That said...

    >I really don't see why you shouldn't do it the way you have it
    >coded here.

    because, one, I might not know what the variable names are (they may be variables... as alluded to below), and two, it just gets tiring typing out a hash structure especially if a lot of fields are involved. Tom's suggestion below works well for such a problem.

    Thanks though for you help.

      I really don't see why you shouldn't do it the way you have it coded here.
      because, one, I might not know what the variable names are (they may be variables... as alluded to below), and two, it just gets tiring typing out a hash structure especially if a lot of fields are involved. Tom's suggestion below works well for such a problem.

      Why are the variable names important if you know what each column represents? A hash structure is indeed useful for HTML::Template so you can refer to that data by name in the template, which is, for most people's purposes, completely separate from the code. (In templates there is no guarantee that the person editing the template will know anything about structures in the code.) But with the AofA, you know that $crazypersonarray->[0] is a lastname *variable* and so on. You could even assign $ln = 0 and refer to it as $crazypersonarray->[$ln]. Your array of hashes can be accessed using something like print $hash->{ln} for my $hash ( @aoh );. That said, I think you should use what feels intuitively "right". :)

      --
      Allolex

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found