The homework assignment seemed easy enough, create a class
that reads in a file, generates a hash file, actually HOH,
passes it back and do stuff to the hash file. I thought I had
everything working until I decided to test out creating a
new OBJ from an existing one. Actually not a part of the
assignment, but we were supposed to allow for it, and having
written the code I thought I'd test it, only to find out that
having created the hash, returned it to main, and called
another method the Hash seemed to have dissapeared. The basic
print statements say that I'm generating a hash, it's the same
hash in main, and the again the same has in the module. But
when I try to access the data in it, nothings there. With
not enough PERL under my belt yet, I can't see what must be
something basic that I've overlooked. Perhaps someone can
be of assistance, as nothing in the FAQ's seem to deal with
thos particular problem. Thanks in advance for any advice
you can offer.
#!/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;
}
Data would be State:city:1.2 2.1 3.4 ...
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.