Go Back   Talk Root - PC Hardware, Software and Web Development forums > Internet & Website Management > Programming Discussion > Perl



Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1  
Old 01-14-2004, 06:50 AM
Bruno Boettcher
Guest
 
Posts: n/a
Default making a copy of a hash by reference?

Hello

i have set up an alias system for a DB like reference system. This means
that i have a set of records that might be manipulated and a delegation
function that returns a given data record by key.

Now in certain cases, mainly displaying, it is interesting to know if
the given record is an alias or a plain record, In the database i hold
for aliases a reference on a hash, which represents the datarecord.

the delegator that returns the hash ny bame makes the test if its an
alias, in which case till now it returned the record, otherwise returns
directly the record.

Now i wanted to change this and create a new record, means a new hash,
with inside references onto the original record (that way that when
manipulating this record the original dataset is changed) and an
additional key indicating that this record is in fact an alias.

most fields in the dataset-hash are themselves references, so passing
them to the new hash doesn't make any problems, problems arise for the
fields that are in fact scalars....

and i have no idea on how to specify to pass the scalar inside of a hash
by reference and not by value...

this:
$tmpRes->{$entry} = \{$tmpRet->{$entry}};
doesn't work....

any help welcome

--
ciao bboett
================================================== ============
[email]bboett@adlp.org[/email]
[url]http://inforezo.u-strasbg.fr/~bboett[/url]
Reply With Quote
  #2  
Old 01-14-2004, 07:18 AM
mj
Guest
 
Posts: n/a
Default Re: making a copy of a hash by reference?

Bruno Boettcher wrote:
> and i have no idea on how to specify to pass the scalar inside of a hash
> by reference and not by value...
>
> this:
> $tmpRes->{$entry} = \{$tmpRet->{$entry}};
> doesn't work....


I'm not sure that I follow you, but does

$tmpRes->{$entry} = \$tmpRet->{$entry};

or

$tmpRes->{$entry} = \($tmpRet->{$entry});

work for you?

Reply With Quote
  #3  
Old 01-14-2004, 02:34 PM
Beable van Polasm
Guest
 
Posts: n/a
Default Re: making a copy of a hash by reference?

[email]bboett@bboett.dyndns.org[/email] (Bruno Boettcher) writes:
>
> most fields in the dataset-hash are themselves references, so passing
> them to the new hash doesn't make any problems, problems arise for the
> fields that are in fact scalars....


I'd need to see more of your code to understand exactly what you're
doing and why it doesn't work. But here's an idea, why not store
scalars as references-to-scalars instead, so then everything stored in
your hash is a reference, and therefore will work ok.


#!/usr/bin/perl

use strict;
use warnings;

my %hash = ();
my %hash2 = ();
my $scalar = "hello";

$hash{xxx} = \$scalar;
$hash2{zzz} = $hash{xxx};
${$hash2{zzz}} = "goodbye";

print("hash{xxx} = ", ${$hash{xxx}}, "\n");
print("hash2{zzz} = ", ${$hash2{zzz}}, "\n");
print("scalar = $scalar\n");

__END__


--
Tabs are 8 characters. They are NOT adjustable. Never have been, never
will be. Anybody who thinks tabs are anything but 8 chars apart is just
wrong. It's that simple. -- Linus Torvalds
[url]http://beable.com/[/url]
Reply With Quote
Reply

Bookmarks
Sponsored Links
FatCow $88 Plan for $66 only Host Unlimited Domains on 1 Account Professional Hosting from Just Host


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On



All times are GMT -7. The time now is 10:46 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
© 2008 TalkRoot.com