copyhoh.pl to HTML.

index -|- end

Generated: Tue Feb 2 17:54:29 2010 from copyhoh.pl 2009/02/06 1.1 KB.

#!/bin/perl
# NAME: copyhoh.pl
# AIM: copy a hash, into a hash reference, as the value
use strict;
use warnings;
my %collection = ();
sub prt($) { print shift; }
sub get_hash($) {
    my ($hr) = shift;
    my %items = ();
    my ($i, $j, $i2, $j2, $key2, $val2, $key1);
    for ($j = 0; $j < 3; $j++) {
        $j2 = $j + 1;
        %items = ();        # clear the hash
        $key1 = "KEY$j2";
        for ($i = 0; $i < 3; $i++) {
            $i2 = $i + 1;
            $key2 = "key".($j2 * $i2);
            $val2 = "value".($j2 * $i2);
            $items{$key2} = $val2;  # generate an item
        }
        $$hr{$key1} = { %items };   # COPY the HASH, into a HASH REF, as the value
    }
}
sub show_collection($) {
    my ($hr) = shift;
    my $max = scalar keys %$hr;
    prt( "Got $max keys...\n" );
    foreach my $key1 (sort keys %$hr) {
        my $val1 = $$hr{$key1};
        foreach my $key2 (sort keys %$val1) {
            my $val2 = $$val1{$key2};
            prt( "$key1: $key2 = $val2\n" );
        }
    }
}
get_hash( \%collection );
show_collection( \%collection );
exit 0;
# eof - copyhoh.pl

index -|- top

checked by tidy  Valid HTML 4.01 Transitional