Title: RE: dynamic creation for hash of hashes

The problem is that you're not actually making an assignment here:
$target_tbl{
            '$tcol' => { 'ctyp' => '$ttyp' ,
                         'clen' => '$tlen'
                       }
           };

You can do this a couple of ways:

$target_tbl{$tcol} = { 'ctyp' => $ttyp, 'clen' => $tlen};
Note that you do *not* want to put single quotes around variables - that prevents variable interpolation, which means you'll just get the variable name as a string rather than it's value.

Another way (which I prefer) is to use hashrefs (hash reference, or a reference to a hash) all the way, e.g.:

my( $target_tbl );

$target_tbl->{$tcol}->{'ctype'} = $ttyp;
$target_tbl->{$tcol}->{'clen'}  = $tlen;

Or a basically equivalent way:

$target_tbl->{$tcol} = { 'ctyp' => $ttyp, 'clen' => $tlen};

Why do I prefer using hashrefs rather than hashes of hashes? Well, hashes of hashes really are hashrefs (other than the actual variable, which is a hash), but there are some other advantages to using a hashref:

- Easier to pass around as arguments to subs
- I find the arrow syntax much clearer and more comprehensible, particularly when you get several levels deep. An example from the code I'm working on right now:

$STATS->{$var_name}->{HOURLY}->{$hour}->{max} = $hour_hi;

If that was done using an ordinary hash of hashes, it would look like this:

$STATS{$var_name}{HOURLY}{$hour}{max} = $hour_hi;

- Once you start building objects you're going to be using hashrefs a lot anyway, so why not learn them now?

Hope this helps,

Barry

________________________________________
From: Sabherwal, Balvinder (MBS) [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, June 15, 2004 01:12 PM
To: [EMAIL PROTECTED]
Subject: dynamic creation for hash of hashes

Guru's

Can some one pls. point out why my code is not working? I'm trying to create a hash of hashes dynamically ar the runtime with the code below.


Thanks

while ( <TL> )
{
�� chomp();
�� $tdata = $dbhs->ct_sql("select o.name , c.name , t.name,c.length
������������������������ from $opt_d..sysobjects o, $opt_d..syscolumns c, $opt_d..systypes t
������������������������ where o.name = \"$_\" and o.uid = 1
������������������������������ and o.id = c.id
������������������������������ and c.usertype = t.usertype
������������������������ order by c.name"
����������������������� );� ## Get the target columns

� if ( defined($errmsg) )�������� ## Make sure there were no errors in the last sql execution.
� {
�� msg_send("$opt_N","AlterRepDef.pl Failed for $opt_d db on $opt_s server" , "Script failed due to errors below.\n$errmsg\n","

$mailto","$pto");
�� exit(1);
� }
� $tlen = scalar(@$tdata) ; ## Get # of columns for the target table.
� foreach my $r (@$tdata)
� {
���� my ( $ttb , $tcol , $ttyp, $tlen ) = @$r;
���� $target_tbl{
������������������ '$tcol' => { 'ctyp' => '$ttyp' ,
��������������������������������������� 'clen' => '$tlen'
������������������������������������� }
��������������� };
���� push(@t_table,@$r);
� }
}

use Data::Dumper;
Dumper(%target_tbl);



***********************************************************************************
This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secured or error-free as information could be intercepted, corrupted, lost, destroyed, received late or incomplete, or could contain viruses. The sender therefore does not accept liability for any error or omission in the contents of this message, which arises as a result of e-mail transmission. If verification is required, please request a hard-copy version from the sender.
***********************************************************************************
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to