Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Re: Double Hash Key

by 3dbc (Monk)
on Jan 20, 2004 at 00:59 UTC ( [id://322496]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re:^4 Double Hash Key
by Roy Johnson (Monsignor) on Jan 20, 2004 at 16:46 UTC
    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); }

        I decided to widdle your code to:

        sub getTop25{ my $dbh = DBI->connect('DBI:ODBC:cdratelistingservice','$db','$pss +wd'); # ODBC connect print "Error opening database: $DBI::errstr\n" and exit unless $db +h->ping; my $connected = $dbh->ping; $dbh->{FetchHashKeyName}='NAME_lc'; my @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" ); for my $i (0..$#fields) { %complex = %{ merge( \%complex, $dbh->selectall_hashref( "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", "key" ) ) }; } print "\n\t",scalar keys %complex," RECORDS Found\n"; } sub GetStationNumbers{ my $dbh = DBI->connect('DBI:ODBC:poop'); # ODBC connect print "Error opening database: $DBI::errstr\n" and exit unless $dbh- +>ping; my $connected = $dbh->ping; $dbh->{FetchHashKeyName}='NAME_uc'; foreach my $key (sort keys %complex) { %complex = %{ merge( \%complex, $dbh->selectall_hashref( "SELECT station_number FROM inst where key = '$key'", # $key? "key" ) ) }; } $HoH_ref = \%complex; # $HoH_ref is a global? return scalar keys %complex; }

        You seem to use globals quite excessively. This is usually considered to be a bad thing unless you explicitly wish to offer access through through them. Of course, you may be declaring these globals with my which is a very bad thing if you ever wish to move to mod_perl. A problem that exists with both of our code is that we fail to check whether or not the database queries are successful or not. I'm also curious, are you using strict and warnings?

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

Log In?
Username:
Password:

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

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

    No recent polls found