By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

Perl Pointer Question

P: 4
Hi,

I was wondering if someone could figure out why my pointer assignment below won't work!
In my code, I try to create a new hash %newhash at the same location as %hashy using the following code:
%newhash=%$var;
However, if you run the following code you will notice that \%hashy and \%newhash do not contain the same address values.

This is wrecking my head so any help would be much appreciated!
Thanks in advance!

Note: To run it, you'll need a file named, ph_nos, containing data like the following:
Expand|Select|Wrap|Line Numbers
  1. sdfasd,sfads
  2. sdfasdf,sdfasd
  3. sdfasd,sdfads
  4. I.e. 2 strings seperated by commas.
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl/
  2.  
  3. open(FILEREAD,"ph_nos");
  4. @file_in=<FILEREAD>;
  5. foreach $elem (@file_in)
  6.    {
  7.    @temp=split(",",$elem);
  8.    %hashy = @temp;
  9.    $var = \%hashy;
  10.    print \%hashy;
  11.    }
  12.  
  13.     %newhash=%$var;
  14.    print $var;                       #should be same val as below but it isn't?????
  15.    print \%newhash;
Apr 1 '08 #1
Share this Question
Share on Google+
5 Replies


nithinpes
Expert 100+
P: 410
Hi,

I was wondering if someone could figure out why my pointer assignment below won't work!
In my code, I try to create a new hash %newhash at the same location as %hashy using the following code:
%newhash=%$var;
However, if you run the following code you will notice that \%hashy and \%newhash do not contain the same address values.

This is wrecking my head so any help would be much appreciated!
Thanks in advance!

Note: To run it, you'll need a file named, ph_nos, containing data like the following:
sdfasd,sfads
sdfasdf,sdfasd
sdfasd,sdfads
I.e. 2 strings seperated by commas.


#!usr/bin/perl/

open(FILEREAD,"ph_nos");
@file_in=<FILEREAD>;
foreach $elem (@file_in)
{
@temp=split(",",$elem);
%hashy = @temp;
$var = \%hashy;
print \%hashy;
}

%newhash=%$var;
print $var; #should be same val as below but it isn't?????
print \%newhash;
That is the expected result. In the line:
Expand|Select|Wrap|Line Numbers
  1. %newhash=%$var;
  2.  
the memory location/address of the hash is not getting copied. Rather, you are copying the content of the hash. $var is being dereferred and the resulting hash is assigned to %newhash.
If you need to copy the reference address, you should try this way:
Expand|Select|Wrap|Line Numbers
  1. $newref = $var; #assigning reference
  2. print "old reference:$var\n newreference:$newref\n";
  3.  
  4. ## Dereference
  5.  
  6. print "The contents of the hash:\n";
  7. print "$_ : $var->{$_}\n" foreach(keys %$var);
  8. print "$_ : $newref->{$_}\n" foreach(keys %$newref);
  9.  
Apr 1 '08 #2

P: 4
Hi,
Thanks so much for your quick reply.
Although, im not so sure that im interpreting you correctly.
If I am, then this suggest that if i code the following:
print keys(%newhash)..
this should print the same as
print keys(%hashy)
.. but this is not the case.

Am I missing something?

Thanks again for your help.
Apr 1 '08 #3

P: 4
Wait I think I've figured it out.
I didn't realise the hash would be destroyed when I went outside the scope of the for loop.
So this explains the query in my last post!

If I declare %newhash as a global variable and assign %newhash within my for loop as follows:
%newhash=%$var;
..will the hash structure exist at address &newhash outside the scope of the for loop or will it also be destroyed, like %hashy?

I know this is terrible programming but I just want to figure out how hash structured are created/destroyed/referenced in memory.

Thanks, once again!
Apr 1 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
In the code:

Expand|Select|Wrap|Line Numbers
  1. open(FILEREAD,"ph_nos");
  2. @file_in=<FILEREAD>;
  3. foreach $elem (@file_in)
  4. {
  5. @temp=split(",",$elem);
  6. %hashy = @temp;
  7. $var = \%hashy;
  8. print \%hashy;
  9. }
  10.  
%hashy is overwritten each time by this line:

%hashy = @temp;

in the end %hashy will only equal whatever the last line of the file was. That might be contributing to some confusion on your part if you think %hashy should have all the lines of the file as key/value pairs.

SInce you are not using "strict" or even "my" all your variables are global, so there is no issue of variable scoping. Declaring a variable inside the foreach loop will not affect it outside the loop. The real problem is most likely what I described above.
Apr 1 '08 #5

P: 4
Yup, that's sorted my confusion alright!
Thanks for all your help!
Apr 2 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.