Thread: Refernce

Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0

    Refernce


    Hi all,
    i want to do something simple..
    i want to save refences to keys in hash as reference array..
    For example if my hash:
    Y -> 5
    X -> 7
    Z -> 8

    and i want to save refernces of Y & Z in array. (refY,refZ)
    how can i do that? Or, why this not working?

    array[0] = &{$hash->{'Y'}};
    array[1] = &{$hash->{'Z'}};

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    You could create an anonymous hash and use a scalar variable to refer to this hash. Then you can access each value in hash by de-referencing.

    Code:
    #! /usr/bin/perl
    use strict;
    use warnings;
    
    my $hashRef = { 'x' => 5, 'y' => 6, 'z' => 7 };
    my @array;
    
    $array[0] = $hashRef->{x};
    $array[1] = $hashRef->{y};
    $array[2] = $hashRef->{z};
    
    print "@array\n";
  4. #3
  5. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,264
    Rep Power
    1810
    Your syntax for creating a hash is wrong. Your syntax for referring to an element in an array is wrong. Your syntax for making a reference is wrong.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    print "create a hash\n";
    
    my %hash = (
    	Y => 5,
    	X => 7,
    	Z => 8
    );
    
    print Dumper \%hash;
    
    print "create an array ";
    
    my @array;
    
    print "and store two references to data in the hash inside of it\n";
    
    $array[0] = \$hash{'Y'};
    $array[1] = \$hash{'Z'};
    
    print Dumper \@array;
    
    print "change data in the original hash\n";
    
    $hash{'Y'} = 'Y';
    $hash{'Z'} = 'Z';
    
    print "and see that the data in the array has also changed\n";
    
    print Dumper \@array;
    
    print "de-reference to get to the values inside\n";
    print "${$array[0]} ${$array[1]}\n";
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Well, the suggested solutions might be what the OP really wanted, but their are not what the OP actually asked for.

    The OP requested an array of references to the keys of the hash, not to the values of the hash.

    If an array of references to the keys of the hash is what is needed, this session under the Perl debugger shows how to do it:

    Code:
      DB<1>  %h = (jan => 1, feb => 2, mar => 3)
      DB<2> push @c, \$_ foreach  keys %h
      DB<3> x @c
    0  SCALAR(0x4a29a4)
       -> 'feb'
    1  SCALAR(0x4a1b40)
       -> 'jan'
    2  SCALAR(0x4a0568)
       -> 'mar'
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    I will try to explain in more details, what i am trying to do.
    i have 2 hashes that i am using as reference.
    Every use of the program i am getting a new file that i want to check which part of it found in the references hashs.
    For example, i have those 2 hashes:
    Code:
    my %hash_a = (
    	AAAAAAAAAAAAAA => 1,
    	BBBBBBBBBBBBBB=> 2,
    	CCCCCCCCCCCCCC => 3
    );
    my %hash_b = (
    	DDDDDDDDDDDDDD => 1,
    	EEEEEEEEEEEEEE => 2,
    	FFFFFFFFFFFFFF => 3
    );
    In the first use i am storing those hashs in the memory..
    (because of the reuseable, the original hashs are larger ~10GB, that is why i retrive every time just 1 hash and then undef it before retrieving the second..)
    Code:
    store(\%hash_a, "hash_a");
    store(\%hash_b, "hash_b");
    Now, i am getting a new file and i want to check which words find in hash_a and which in hash_b and which are not found at all.
    word define to be for example:
    word1:AAAAAAAAAAAAAA
    word2:EEEEEEEEEEEEEE

    My new file can be like:
    AAAAAAAAAAAAAA
    EEEEEEEEEEEEEE
    KKKKKKKKKKKKKK

    The desire output is array:
    array[0]: refernce to word1 in hash_a in the memory (key)
    array[1]: refernce to word1 in hash_b in the memory (key)
    array[2]: refernce to word1 in hash_unfound in the memory (key)

    What i did is a "for loop" that first check all the new words if they find in hash_a, if they found save the refernce, otherwise, sign it as 'u' (unfound) and keep it in hash of unfound.
    Then look for the words in the hash_b with the same procedure.

    Code:
    my $new_file;
    my @array;
    my $array_index=0;
    my @array_type;
    my $hash_a;my $hash_b;
    my %hash_unfound=();
    my $i_unfound_file=1;
    my $x=0;
    
    for(my $i=0;$i<2;$i++)
    {
            open ( $new_file ,$ARGV[0]) || die ("can't open file $ARGV[0] file");
            while (<$new_file>)
            {
                chomp $_;
                my $word=$_;
                if ($i==0)
                {
                    if ($x==0)
                    {
                        $hash_aaa = retrieve("hash_a") ;
                        $x=1;
                    }
                    if (exists  $hash_aaa->{$word})
                    {
                        $array[$array_index]=\$hash_aaa{$word};
                        $array_type[$array_index]='a';
                    }
                    else
                    {
                        $array_type[$array_index]='u';
                        $hash_unfound{$word} = $i_unfound_file;
                        $array[$array_index]=\$hash_unfound{$word};
                        $i_unfound_file++;
                    }
                }
    
                if ($i==1)
                {
                    if ($x==1)
                    {
                        undef $hash_a;
                        $hash_bbb = retrieve("hash_b") ;
                        $x=2;
                    }
                    if (exists  $hash_bbb->{$word} )
                    {
                        $array[$array_index]=\$hash_bbb{$word};
                        $array_type[$array_index]='b';
                    }
                    else
                    {
                        $array_type[$array_index]='u';
                        $hash_unfound{$word} = $i_unfound_file;
                        $array[$array_index]=\$hash_unfound{$word};
                        $i_unfound_file++;
                    }
                }
                $array_index++;
            }
    }
    Hope it more clear now..
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Ahhh, my code does not working..
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Originally Posted by papori
    Ahhh, my code does not working..
    This a poor description of the problem which does not really help us to help you. Does your code compile? If yes, then you did not show us all your code, because I don't think that what you've shown will compile. If not, what errors do you get?

    Generally speaking, your code seems to be uselessly complicated to do things that could most probably be simpler.
  14. #8
  15. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,264
    Rep Power
    1810
    The term 'reference' has a very specific meaning in perl, and I don't think you are using it in the way we understand.

    It seems you just want to check existence. Once again, I recommend you don't load an entire 10GB data set every time you want to ask the question, especially if you are going to be swapping the data in and out a lot. For one thing, you will lose any speed advantage memory has over disk IO.

    A database would be easier and better. My opinion only of course, but if you provide a real data set (even just ten to twenty lines), I'll provide an example of how it could work.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    Sorry for this..
    I am new to Perl, so i might be wrong..
    Lets try to do it step by step..

    i am trying to save array which every cell of it hold reference to hash key, not the value.
    i am doing it as :
    Code:
    $array[$array_index]=\$hash{$word};
    in the next step, i just want to print the hash key from the array.
    i tried:
    print ${$array[1]};
    and many others.. but with no succesfull..
    Any idea?

    Thanks
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Code:
    $array[$array_index]=\$hash{$word};
    This is creating a reference to the hash value, not to the hash key.

    If you want references to the hash keys use the example I gave you already:

    Code:
      DB<1>  %h = (jan => 1, feb => 2, mar => 3)
      DB<2> push @array, \$_ foreach keys %h
      DB<3> x @array
    0  SCALAR(0x4a29a4)
       -> 'feb'
    1  SCALAR(0x4a1b40)
       -> 'jan'
    2  SCALAR(0x4a0568)
       -> 'mar'
      DB<4>
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2010
    Posts
    22
    Rep Power
    0
    i want to push specific key to specific cell..
    How can i do that using your example?
  22. #12
  23. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,264
    Rep Power
    1810
    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:

    Every use of the program i am getting a new file that i want to check which part of it found in the references hashs.

    ...

    In the first use i am storing those hashs in the memory..
    (because of the reuseable, the original hashs are larger ~10GB, that is why i retrive every time just 1 hash and then undef it before retrieving the second..)
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    If you want the values of your keys, simply do this:

    Code:
    push @array, keys %hash;
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,967
    Rep Power
    1225
    Originally Posted by Laurent_R
    If you want the values of your keys, simply do this:
    Code:
    push @array, keys %hash;
    Why use push? A simple assignment is all that's needed.

    Code:
    my @keys = keys %hash;
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Yes, sure an assignement is sufficient if it is a new array. No if you want to add something to an existing array.
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo