in reply to Re: Double Hash Key
in thread Double Hash Key

Why are you taking a ref to the hash?

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
Re: Re: Re: Double Hash Key
by 3dbc (Monk) on Jan 20, 2004 at 00:59 UTC
    Why Not? It adds another level of data abstraction. The entire data structure can now be considered an object. Isn't a complex data structure pointer considered to be better programming style?

    Thanks,
    -3dbc
      If the data structure had methods, it could be considered an object. I don't think there's any advantage to merely operating through a reference when you can do exactly the same thing on the structure. There's nothing that says an object needs to be a reference, although that's how they are usually implemented.

      The PerlMonk tr/// Advocate
        Here is my example demonstrating an advantage to merely operating through a reference.
        I would find it to be much more tedious to do exactly the same thing on without a reference.

        use Hash::Merge qw( merge ); sub getTop25{ my ($dbh, $sth); $dbh = DBI->connect('DBI:ODBC:cdratelistingservice','$db','$psswd' +); # ODBC connect unless ($dbh->ping) { print "Error opening database: $DBI::errstr\n"; exit; } my $connected = $dbh->ping; $dbh->{FetchHashKeyName}='NAME_lc'; @fields = ( "30_day", "60_day", "90_day", "180_day", "270_day", "1 +_year", "18_month", "2_year", "3_year", "4_year", "5_year", "v +ariable_1", "variable_2", "variable_3" ); my $num_fields; $num_fields = @fields; for (my $i=0;$i<$num_fields;$i++) { $sql = "Select TOP 25 $fields[$i], key from cdrates c, main m +WHERE r.key = m.key and m.valid = '$date' ORDER BY $fields[$i] DESC"; push @sql, $sql; } # @hashref demonstrates the advantage to merely # operating through a reference. $num_queries = @sql; for (my $j=0;$j<$num_queries;$j++) { # update to check whether or not the database queries are # successful or not my $hashref = $dbh->selectall_hashref($sql[$j], "key") or die +$dbh->errstr; push @hashref, $hashref; } # here i merge all hashes in order to display only # 1 instance of an institution that is posting a top25 # jumbo CD rate in any of the above catagories (fields) $num_hashref = @hashref; for (my $k=0;$k<$num_hashref;$k++) { %complex = %{ merge( \%complex, \%{$hashref[$k]} ) }; } my $num_merge=scalar(keys %complex); print "\n\t$num_merge RECORDS Found\n"; } sub GetStationNumbers{ my ($dbh, $sth); $dbh = DBI->connect('DBI:ODBC:poop'); # ODBC connect unless ($dbh->ping) { print "Error opening database: $DBI::errstr\n"; exit; } else { #print "\n\nQuerying ACT! dBASE DB\n\n"; } my $connected = $dbh->ping; $dbh->{FetchHashKeyName}='NAME_uc'; my $sql; my @sql, @hashref; foreach my $key (sort keys %complex) { $sql = "SELECT station_number FROM inst where key = '$key'"; push @sql, "$sql"; } my $num_qeries; $num_queries = @sql; for (my $j=0;$j<$num_queries;$j++) { # update to check whether or not the database queries are # successful or not my $hashref = $dbh->selectall_hashref($sql[$j], "key") or die +$dbh->errstr; push @hashref, $hashref; } my $num_hashref; $num_hashref = @hashref; for (my $k=0;$k<$num_hashref;$k++) { %complex = %{merge( \%complex, \%{$hashref[$k]} )}; # I use a reference here just because I felt like it. $HoH_ref = \%complex; } my $num_merge=scalar(keys %complex); }