Perl Pointer Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laurar
    New Member
    • Apr 2008
    • 4

    #1

    Perl Pointer Question

    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:
    Code:
    sdfasd,sfads
    sdfasdf,sdfasd
    sdfasd,sdfads
    I.e. 2 strings seperated by commas.
    [CODE=perl]
    #!usr/bin/perl/

    open(FILEREAD," ph_nos");
    @file_in=<FILER EAD>;
    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;[/CODE]
    Last edited by eWish; Apr 1 '08, 11:04 PM. Reason: Please use code tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    Originally posted by laurar
    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=<FILER EAD>;
    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:
    Code:
    %newhash=%$var;
    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:
    [CODE=perl]
    $newref = $var; #assigning reference
    print "old reference:$var\ n newreference:$n ewref\n";

    ## Dereference

    print "The contents of the hash:\n";
    print "$_ : $var->{$_}\n" foreach(keys %$var);
    print "$_ : $newref->{$_}\n" foreach(keys %$newref);
    [/CODE]
    Last edited by eWish; Apr 1 '08, 11:05 PM. Reason: Fixed Code Tags

    Comment

    • laurar
      New Member
      • Apr 2008
      • 4

      #3
      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.

      Comment

      • laurar
        New Member
        • Apr 2008
        • 4

        #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!

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          In the code:

          Code:
          open(FILEREAD,"ph_nos");
          @file_in=<FILEREAD>;
          foreach $elem (@file_in)
          {
          @temp=split(",",$elem);
          %hashy = @temp;
          $var = \%hashy;
          print \%hashy;
          }
          %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.

          Comment

          • laurar
            New Member
            • Apr 2008
            • 4

            #6
            Yup, that's sorted my confusion alright!
            Thanks for all your help!

            Comment

            Working...