Thread: Refernce

    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Thank you for your help.
    i dont know if i am missing somthing from your explains, but i dont want all the keys to be in array..
    let say i want only 1 out of 10.
    i want reference only to specific key to be save in specific cell at array.
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Originally Posted by keath
    Are you sure you want a reference, and not just the value of the key?

    Code:
    $array[$array_index] = $word;
    I'm trying to follow the logic of what has been said:



    A reference is a pointer to a memory location, but once you undef that pointer will be meaningless. Even reloading the same hash probably won't give the same result twice.
    Yes, i am sure i want a reference and not the value..
    I am storing a hash in the memory, so when i am using undef, the pointer is meaningless only for the current executin,next time when i have to use it i can retrieve the hash and the pointer will became full meaning.
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Originally Posted by papori
    Thank you for your help.
    i dont know if i am missing somthing from your explains, but i dont want all the keys to be in array..
    let say i want only 1 out of 10.
    i want reference only to specific key to be save in specific cell at array.
    Well, this was just an example, but if you want only some, then just push into your array the keys you want to store. I cannot guess which ones you want to store in there, you haven't told us if I did not miss out something.

    .
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Originally Posted by Laurent_R
    Well, this was just an example, but if you want only some, then just push into your array the keys you want to store. I cannot guess which ones you want to store in there, you haven't told us if I did not miss out something.

    .
    This is what i am doing..
    Code:
    if ( exists($hash->{$word}) )
    {
          $array[$array_index]=\$hash->{$word};
    }
    If this key exists, save reference to that key in the array.
    For now, it save reference to the value.
    I just want to change the second line in the code i just posted..
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Perl Code:
    if ( exists($hash->{$word}) )
    {
          $array[$array_index]=\$hash->{$word};
    }


    Why are you using -> between your hash name and the key. Are you using a hash ref or a hash?

    If you want to store a reference to the key simply do this:

    Perl Code:
    if ( exists($hash->{$word}) )
    {
          $array[$array_index]=\$word;
    }


    But I am not sure why you want a reference to the key, rather than simply the key itself. All this sounds like an XY problem: you want to solve X and you ask Y. It would be better if you really told us what you want, rather and asking us to help you on how you think it should be done, whereas there might be better ways to do it..
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Originally Posted by Laurent_R
    Perl Code:
    if ( exists($hash->{$word}) )
    {
          $array[$array_index]=\$hash->{$word};
    }


    Why are you using -> between your hash name and the key. Are you using a hash ref or a hash?

    If you want to store a reference to the key simply do this:

    Perl Code:
    if ( exists($hash->{$word}) )
    {
          $array[$array_index]=\$word;
    }


    But I am not sure why you want a reference to the key, rather than simply the key itself. All this sounds like an XY problem: you want to solve X and you ask Y. It would be better if you really told us what you want, rather and asking us to help you on how you think it should be done, whereas there might be better ways to do it..
    Yes, this is a hash ref.
    You gave me a reference to the word, and not to the hash key.
    i have a list of words, that i am checking their existence in the hash, if its there save a reference to the hash key..
  12. #22
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Originally Posted by papori
    You gave me a reference to the word, and not to the hash key..
    But $word is your hash key.
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Originally Posted by Laurent_R
    But $word is your hash key.
    No.
    The word is just word that i want to check if whether its found in hash keys.
    That is why i want the ref and not the key itself.
    i want a ref to the memory, because the hash stored in the memory, the word not.
  16. #24
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    if this:

    Perl Code:
    if ( exists($hash->{$word}) )


    returns true, then it means that whatever is the value of $word is a key to your hash. You can either store the value of the key directly into your array (push @array, $word) or a ref to $word, meaning that of you later change $word the value refered into your array will also change.

    You probably want to do the first of the two things.
  18. #25
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Originally Posted by Laurent_R
    if this:

    Perl Code:
    if ( exists($hash->{$word}) )


    returns true, then it means that whatever is the value of $word is a key to your hash. You can either store the value of the key directly into your array (push @array, $word) or a ref to $word, meaning that of you later change $word the value refered into your array will also change.

    You probably want to do the first of the two things.
    In your example, if i will change the word, i will not change the key..

    i will try to explain by example, if i have this hash:
    my %hash_a = (
    AAAAAAAAAAAAAA => 1,
    BBBBBBBBBBBBBB=> 2,
    CCCCCCCCCCCCCC => 3
    );

    How can i get ref to the second key? if i have word that is exactly like the hash key:
    BBBBBBBBBBBBBB

    i want to controll the hash and not the word.
  20. #26
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Alright, once more.

    If you have:

    Code:
    if ( exists($hash->{$word}) )
    returning true, then it means that the $word variable happens to contain at that moment a value that is a key to your hash, possibly "BBBBBBBBBBBBBB", and the value of your hash for that key is 2. I understand that you want to store that key, i.e. the "BBBBBBBBBBBBBB" value. If this is what you_ want to do, then you have to do this:

    Code:
    push @array, $word;
    Your array will now contain one element whose value is "BBBBBBBBBBBBBB", i.e. a key of your hash. Even if the $word variable changes later, your array will continue to contain "BBBBBBBBBBBBBB". I would think that is probably what you need.

    But if you store in your array a reference to the $word variable (with a command such as "push @array, /$word;"), then, your array will contain a reference to $word, which means that, if your program is later giving a different value to the $word variable, your array element will now refer to that new value. That is probably not what you want.
  22. #27
  23. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,254
    Rep Power
    1810
    Originally Posted by papori
    Yes, i am sure i want a reference and not the value..
    I am storing a hash in the memory, so when i am using undef, the pointer is meaningless only for the current executin,next time when i have to use it i can retrieve the hash and the pointer will became full meaning.
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Storable qw(store retrieve);
    use Data::Dumper;
    
    my %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1);
    store(\%color, 'mycolors') or die "Can't store %a in mycolors!\n";
    
    my $colref;
    for (1 .. 10) {
    	$colref = retrieve('mycolors');
    	print "$colref\n";
    	undef $colref;
    }
    Result on my machine:
    HASH(0x7fee6182bc10)
    HASH(0x7fee61831318)
    HASH(0x7fee6182bb98)
    HASH(0x7fee6182bb68)
    HASH(0x7fee6182bb80)
    HASH(0x7fee6182bc10)
    HASH(0x7fee61831318)
    HASH(0x7fee6182bb98)
    HASH(0x7fee6182bb68)
    HASH(0x7fee6182bb80)
    It sounds as if you believe the hash has some fixed memory location it will be loaded into, but it doesn't work that way.

    The value of the hash key is how you find your data. The address is not fixed, and can not be known to you ahead of time.
  24. #28
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Originally Posted by keath
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Storable qw(store retrieve);
    use Data::Dumper;
    
    my %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1);
    store(\%color, 'mycolors') or die "Can't store %a in mycolors!\n";
    
    my $colref;
    for (1 .. 10) {
    	$colref = retrieve('mycolors');
    	print "$colref\n";
    	undef $colref;
    }
    Result on my machine:


    It sounds as if you believe the hash has some fixed memory location it will be loaded into, but it doesn't work that way.

    The value of the hash key is how you find your data. The address is not fixed, and can not be known to you ahead of time.
    Thank you keath!
    This is exactly what i believed..
    And now i believe that i cant in this way, or is there any way to determine the hash to permanent location in the memory?..
    How can i solve that problem when i need "long term pointers"? (pointers that will be relevant also in the next executing of the program)
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    How can i solve that problem when i need "long term pointers"? (pointers that will be relevant also in the next executing of the program)
    You can't. That's not how RAM works.

    It's clear that you definitely have an XY problem. You should stop asking how to implement your faulty Y solution and instead ask how to solve the real problem.

    What is the real problem that you're needing to solve?
  28. #30
  29. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,254
    Rep Power
    1810
    I will take more time tonight to try and understand the requirements of your program. Right now, I'm not clear what you are trying to accomplish.

    But personally, I would store the key, which is a relative pointer from the current hash location to your data. It will always know how to find what you want from wherever the hash is loaded.

    I don't yet understand why that won't work for you.

    And I'm still strongly inclined to believe a database would be a better solution to this problem. The reason is that the data would be indexed to a location in the file rather than memory.

IMN logo majestic logo threadwatch logo seochat tools logo