473,396 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Perl Pointer Question

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
5 1643
nithinpes
410 Expert 256MB
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
laurar
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
laurar
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
4,059 Expert 2GB
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
laurar
4
Yup, that's sorted my confusion alright!
Thanks for all your help!
Apr 2 '08 #6

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

Similar topics

3
by: Helmut Jarausch | last post by:
Hi, having been a Perl fan for years I have nearly converted to Python. Still, there are LOTS and very good modules written in Perl. Is there a tool to lesson the burdon of translation of Perl...
2
by: James Hull | last post by:
Hi All: I am new in the Perl world. So far I have installed cygwin and Perl 5.8.0 along with Perl DBI and DBD::Oracle modules. NowI am trying to install Perl Tk on my PC (Windows 2K). I have...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
2
by: Calvin | last post by:
use AI::Genetic; my $ga = new AI::Genetic( -fitness => sub { rand }, -type => 'bitvector', -population => 500, -crossover => 0.89, -mutation => 0.01, -terminate => sub {...
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
35
by: lnatz | last post by:
Hi, Is there an equivalent to the perl command chomp in C? And if there is no exact equivalent command, how would I go about removing the "\n" at the end of a stdin? Thank you, Natalie
0
by: srinu123 | last post by:
Hi all, I am tring the install perl module(GD-2.35) on my linux machine.But i am getting some error...Please find the error below... D.xs:1450: invalid lvalue in assignment GD.xs:1450:...
9
by: grocery_stocker | last post by:
How are references and aliases in perl different than references in aliases in C++?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.