Robertn has asked for the wisdom of the Perl Monks concerning the following question:
Data would be State:city:1.2 2.1 3.4 ...#!/bin/perl use lib ("."); use Rainfall; $weather = Rainfall->new('$infile'); $storms = $weather->new(); # clone Rainfall : #!/bin/perl package Rainfall; sub new { my $type = shift; my $class = ref($type) || $type; my ($States,$other) =({}, {}); my ($infile,$key1,$key2,$ary,$city,$state); my $clone = (["clone"]); if (@_) { $infile = $ARGV[0]; # get input weather filename open (infl,"<$infile") || die "Cannot open $infile \n"; while (<infl>) #read the whole { ($state, $city, $ary) = (split/:/); # speperate the state $state = ucfirst $state; $city = ucfirst $city; $States{$state}{$city} = $ary; #Build hash file } close (infl) || die("$infile can't be closed:$1"); bless $States, $class; print "new is $States\n"; foreach $state (sort keys %States) { foreach $city (sort keys %{$States{$state}}) { $str = join(",",$state,$city); printf "%-30s %s\n ",$str,$States{$state}{$city}; } } } elsif (ref $type) { foreach $key1 (keys %$type) { print "clone k1 $key1\n"; #<=== This never happens foreach $key2 (keys %{$type{$key1}}) { $other{$key1}{$key2} = $type{$key1}{$key2}; print "k1 $key1 k2 $key2 \n"; } } print "cloning $type to $other\n"; bless $other,'Rainfall'; foreach $state (sort keys %other) { foreach $city (sort keys %{$other{$state}}) { $str = join(",",$state,$city); printf "%-30s %s\n ",$str,$other{state}{$city}; } } } else { bless {}, 'Rainfall'; } return ref($type) ? $other : $States; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Obj ref to %hoh
by ChemBoy (Priest) on Oct 28, 2001 at 08:28 UTC | |
by Robertn (Hermit) on Oct 29, 2001 at 02:31 UTC | |
|
Re: Obj ref to %hoh
by Anonymous Monk on Oct 28, 2001 at 07:56 UTC |