Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Wanted further clarification on Data Usage

by Buckaroo Buddha (Scribe)
on Jul 11, 2000 at 18:01 UTC ( #21972=perlquestion: print w/replies, xml ) Need Help??

Buckaroo Buddha has asked for the wisdom of the Perl Monks concerning the following question:

In my talk with davorg he mentioned that i may have had
a bug in my program... here's the setup:

I use a variable named $thisDataset as a pointer
to a Hash table. i then address  %thisDataset
at a later time. Let's clear this up a bit...

my $filehandle = &magic_box(@ARGV); my $thisDataset = &doStuff($filehandle); { my (%HASH); #this wasn't here till after talking to dave :) #if you'd like to comment on the effects with #and without i'd be very interested :) sub doStuff { while(<>) { my @array = split(/,/); my $key = shift @array; $HASH{$key} = [@array]; } return(\%HASH); # this is the same as $thisDataset now } # at least, $thisDataset is the same as } # this... :) &someThingElse($thisDataset); sub someThingElse { foreach my $KEY (sort keys %{$_[0]}) { print "$KEY\n"; foreach my $VALUE (@{$_{$KEY}}) { print "$VALUE,"; } print "\n"; } # in the real world, i do more with this stuff but i } # think this illustrates the point adequately

notice how it's listed as  %{$_[0]} in the second function?
that's the same as saying  %thisDataset (IIUC)
now that's aparently ALSO an error or potential error and i don't understand
exactly why

Replies are listed 'Best First'.
Re: Wanted further clarification on Data Usage
by davorg (Chancellor) on Jul 11, 2000 at 19:05 UTC

    To summarise even further :) The problem I saw was this...

    You have a function which returns a reference to a hash like this:

    sub make_hash { my %hash; # Do clever stuff to fill in values in hash return \%hash; }

    which you call to get a reference to a hash which you store in a scalar called $thisDataset like this:

    my $thisDataset = make_hash();

    Later on you pass this hash to another function which assumes that its first parameter is a hash reference like this:

    sub do_stuff_to_hash { foreach (keys %{$_[0]}) { # do something with each key } } do_something_with_hash($thisDataset);

    All this is fine, but later still you suddenly start to treat $thisDataset as if it's a hash by accesing it like this:

    $thisDataset{$curr_month};

    This is the error that use strict picked up as it realised that you were accessing both the scalar $thisDataset and the hash %thisDataset. I suspect that want you really wanted to do was something like:

    $thisDataset->{$curr_month};

    use strict is your friend. Use use strict.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000, ICA, London
    <http://www.yapc.org/Europe/>
Re: Wanted further clarification on Data Usage
by ZZamboni (Curate) on Jul 11, 2000 at 18:57 UTC
    There's no real question in your post, so I'll just comment on it :-)

    I don't see any real problems with it (other than the fact that I prefer to group function definitions and not mix them with the rest of the code). The my (%HASH) has the effect of localizing the %HASH variable to the block where the doStuff function is declared, effectively making it invisible to everyone else and accessible only through that function. In this sense it is a good thing to have the my statement. If you remove it, the %HASH variable will be accessible from other parts in the program, which may or may not be what you want.

    You mention %thisDataset a couple of times, but there is no such variable in your program. You have %HASH, and you have $thisDataset, which is a pointer to %HASH.

    The use of %{$_[0]} in the second function looks good to me, but the @{$_{$KEY}} does not. I think it should be @{$_[0]->{$KEY}}. It may make things clearer to assign $_[0] to another variable, like this:

    sub someThingElse { my $data=shift; # then you can use %{$data} and @{$data->{$KEY}} }

    --ZZamboni

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://21972]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2019-08-22 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?