List Info

Thread: Help with using sub to update hash




Help with using sub to update hash
user name
2007-03-07 20:58:09
I have the following code

#caller;

my %titles;
my %dvd_titles;
$dvd_act_btn->signal_connect('clicked'=>sub{$count++
&&
&dvd_setup($vbox2,$count,$sel_opts_vbox,%titles,%dvd_t
itles)});

#sub called

sub dvd_setup {
my ($vbox2,$count,$sel_opts_vbox,$titles,$dvd_titles)=_;
my $subname='dvd_setup';
#my %dvd_titles;
my %dvd_titles=%{$dvd_titles};
my %titles=%{$titles};
foreach my $keys (keys %dvd_titles){
print
$keys,"=",$dvd_titles{$keys},"n";
}
print scalar %dvd_titles;

foreach my $titles (sort keys %files){

if (${$files{$titles}}[0] eq $subname){
my $key=${$files{$titles}}[5]->get_active_text;
my $file=${$files{$titles}}[2];
unless (exists($dvd_titles{$key})){
$dvd_titles{$key}
};
push ({$dvd_titles{$key}},${$files{$titles}}[2]);
}
}


foreach my $keys (keys %dvd_titles){
print
$keys,"=",$dvd_titles{$keys},"n";
}

}

However the hashes defined never get updated by the
sub-routine "dvd_setup"

The hash is added to wuthin the sub (see later print lines),
but
original hash is not getting updated.

Any help appreciated.
_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Help with using sub to update hash
country flaguser name
New Zealand
2007-03-08 02:08:38
On Thu, 2007-03-08 at 02:58 +0000, Mike Martin wrote:
> I have the following code

Ouch, that code is really hard to read.  Please tell me
pasting the code
into google mail ate all your indentation and you didn't
actually format
it that way on purpose 

> my %titles;
> my %dvd_titles;

Ok, so these are hashes that exist in the scope of the
caller.

>
$dvd_act_btn->signal_connect('clicked'=>sub{$count++
&&
>
&dvd_setup($vbox2,$count,$sel_opts_vbox,%titles,%dvd_t
itles)});

And when you call dvd_setup you pass it a reference to each
hash - good.

> sub dvd_setup {
> my
($vbox2,$count,$sel_opts_vbox,$titles,$dvd_titles)=_;

Now $titles is a reference to %titles and $dvd_titles is a
reference to
%dvd_titles - also good.

But this is where you go off the rails ...

> my %titles=%{$titles};
> my %dvd_titles=%{$dvd_titles};

Now you've created a new hash called %titles which is scoped
to within
the subroutine and you've *COPIED* the contents of the hash
referenced
by $titles into the new one.  Ditto for %dvd_titles.

You mentioned that updates to these hashes in the subroutine
have no
effect but there are two problems

 1. your sample code never does an update
 2. if it did, it's probably updating the copy rather than
the original

Probably what you want to do is skip the my
%titles=%{$titles}; bit.

Then, to update, instead of doing this:

  $titles{$key} = 'value';

do this:

  $titles->{$key} = 'value';

The arrow (->) means operate on the hash referred to by
$titles.

It just seems like you're not clear on what a hashref is and
the syntax
for using one.  The standard Perl documentation includes a
tutorial on
references:

  h
ttp://search.cpan.org/dist/perl/pod/perlreftut.pod

Since this is not Gtk-specific, can I suggest taking it to
www.perlmonks.org if you need more help.

Regards
Grant

_______________________________________________
gtk-perl-list mailing list
gtk-perl-listgnome.org

http://mail.gnome.org/mailman/listinfo/gtk-perl-list

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )