in reply to Re: getting the name of a variable to use as string
in thread getting the name of a variable to use as string

I'm really trying to avoid heavily repetitious code of the form
$hash{'foo1'} = $foo1; $hash{'foo2'} = $foo2; ... $hash{'foon'} = $foon;
where foo1..foon are column names of a thoroughly denormalized relational table each row of which I want to put into a hash of hashes. Looks like a job for a code generator ;-) Thanks all for your help!

Replies are listed 'Best First'.
Re^3: getting the name of a variable to use as string
by tachyon (Chancellor) on Sep 17, 2004 at 03:08 UTC

    Your thinking appears totally left field. First with DBI you can retrireve rows as hashrefs where the key is already the column name. Next how does the data get into $foo1 in the first place. Why not just put it into a hash in the first instance and then use the hash?

    cheers

    tachyon

      I work in left field ;-). I don't always have access to the data directly from a DBI call. I have to deal with unloads that don't carry column names. Being of the mind that the fewer keystrokes I have to type, the fewer dumb errors I'll have to correct I'm looking for a way to take the results of a split
      my ($a, $b, $c, $d) = split(/\|/,$unloadrow);
      and build a hash keyed by the column name (a, b, etc) without having to type each name twice more as in
      $hash{'a'} = $a; $hash{'b'} = $b; ...
      This may seem lazy but I often have a dozen tables with twenty or more columns to deal with and it would be nice to avoid a lot of error prone and repetitious typing or even cobble up a subroutine to handle building the hash.

      The goal is to build a hash of those hashes so I can look up individual field values to do analysis of the data.

      Thanks!

        As noted before you almost never want to do it the way you described. Hash slice syntax is what you want.

        my %h; my @cols = qw( a b c d ); @h{@cols} = split ' ', 'does this do it?'; use Data::Dumper; print Dumper \%h; __DATA__ $VAR1 = { 'a' => 'does', 'b' => 'this', 'c' => 'do', 'd' => 'it?' };

        If you designed this database it is worth noting that each column in a RDBMS should be atomic ie you should not be storing pipe delimited data and then breaking it out as you loose a lot of power.

        cheers

        tachyon

Re^3: getting the name of a variable to use as string
by sgifford (Prior) on Sep 17, 2004 at 06:07 UTC
    First, as tachyon says, I'm quite curious how you've gotten this information into all these variables, and why you didn't just put them into a hash to begin with. But assuming that's done by somebody else in code you don't control...

    You're approaching the problem backwards. Instead of taking a variable and getting the name out, take the name and turn it into a variable, with a symbolic reference. Something like this should work, though I haven't tested it:

    foreach my $i (qw(foo1 foo2 foo3 ... foon)) { $hash{$i} = $$i; }
      Not only does it not work, but I already explained why in this thread.

      Furthermore if recommending symbolic references I'd strongly suggest mentioning the problems with using them, and also mentioning how to turn off strict.pm for that block (which they should be using).

      I'm trying to put data in the hash as noted in my reply to tachyon. Can't always use DBI (which solves the issue when I can).

      Thanks!