Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: another 'array to hash' question

by caedes (Pilgrim)
on Dec 10, 2003 at 14:31 UTC ( [id://313721]=note: print w/replies, xml ) Need Help??


in reply to another 'array to hash' question

I'm pretty sure that what you're describing is both nearly impossible and something you wouldn't really want to do if you could. In the context of the rest of your code there is probably a much better way of solving the problem, but we can only guess what that is since you havn't provided much other code.

Update: Now that I think about it, accessing the variable names as you descibe is really no different than hard coding the names in an array or just specifying the hash dirrectly. In all cases your still hard coding something, so the results will be the same.

-caedes

Replies are listed 'Best First'.
Re: Re: another 'array to hash' question
by punkish (Priest) on Dec 10, 2003 at 14:55 UTC
    well, the simplest use of what I am looking for would be in converting a csv or tab delim var file into an array of hashes...

    my @a; while (<INFILE>) { my ($fn, $ln, $age) = split("\t", $_); # now convert this array to a hash and stuff it in the array push(@a, {'fn'=>$fn, 'ln'=>$ln, 'age'=>$age}); }

    DBI returns a nicely prepared array of hashes with fetchall_arrayref({}) and I was trying to "emulate" that while using text files. But, "if I could", I would probably find other uses as well.

      Does the CSV have a header line which describes the columns? I often do this
      chomp($headerline = <INFILE>); my @header = split(/\t/,$headerline);
      (although you could just define one relative to the code
      my @header = qw(fn ln age);
      Now when reading in the file
      while( <INFILE>) { chomp; my $c = 0; my @columns = split(/\t/,$_); push @a, { map { $header[$c++] => $_ } @columns }; }
        That's what I would have suggested, with one change: use a hash slice.
        # instead of this my $c = 0; my @columns = split(/\t/,$_); push @a, { map { $header[$c++] => $_ } @columns }; # do this my %row; @row{@header} = split(/\t/,$_); push @a, \%row;
        They're functionally equivalent, I know... but it's just that since what you are doing is really a hash-slice, why not use hash-slice syntax?

        ------------
        :Wq
        Not an editor command: Wq

      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

        >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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found