Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 959179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:59:01+00:00 2026-05-16T00:59:01+00:00

suppose $my_ref = \$hash{‘mary’}; #my_ref is a reference point to a hash element. ….

  • 0

suppose $my_ref = \$hash{'mary'}; #my_ref is a reference point to a hash element.
….
later on, how can I use $my_ref to retrieve the key of the hash element it point to? i.e how to get string ‘mary’ from $my_ref?

I ask this question because I have several groups of user name list, some user names appear in multiple groups which consumes memory. So I decide to create a common user name list, and let these groups only store the reference to the corresponding user name rather than user name.

e.g. originally,

%group1 = {'mary'=>1, 'luke'=1,'tom'=1,...}  
%group2 = {'mary'=>1, 'sam'=1,'tom'=1,...}

Here you see ‘mary’ and ‘tom’ are shown in both group1 and group2 which consume memory. (note I do not care the value in this example, the value is here only because the data struct is a hash). So to reduce memory, I want to have a common list stores all user names:

%common_hash = {'mary'=>1, 'luke'=1,'tom'=1,'sam'=1...};  
$ref1 = \$common_hash{'mary'};  
$ref2 = \$common_hash{'luke'};  
$ref3 = \$common_hash{'tom'};  
$ref4 = \$common_hash{'sam'};

groups only store the reference of the hash element:

%group1 = {$ref1=>1, $ref2=1,$ref3=1,...};  
%group2 = {$ref1=>1, $ref4=1,$ref3=1,...}; 

I think this approach can save much memory because:

  1. one user name is store in memory once not multiple times;
  2. groups stores reference (an integer) rather than string (in my case, the length of each user name is 30 bytes in average, while each integer is only 4 bytes (32 bit sys.) or 8 bytes (64 bit sys.)) (BTW, correct me if an integer does not use 4 bytes or 8 bytes.)
  3. using reference I can access user name immediately without looking for it.

But how can I get the user name from a group?

If I use @my_ref = keys %group1, I think I will get value of ‘mary’,but not ‘mary’.

$result = $($my_ref[0]);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:59:02+00:00Added an answer on May 16, 2026 at 12:59 am

    Sorry, hashes don’t work that way. You aren’t saving any memory by using a reference instead of a string as a hash key, and furthermore you are:

    1. making it harder to find data in the hash (it is obscured)
    2. getting in the way of Perl’s internal hash optimizations (using a hash algorithm to provide O(1) lookup inside what is effectively a list).

    In either case, the hash key is a scalar, which needs to be stored somewhere. By using a reference as the hash key, now you not only need to store the reference in the hash, but also the value it is referencing, so you are now using more memory.

    What led you to believe that you were saving memory by your, cough, novel approach? Have you run a memory profiler against different implementations?

    Generally, you cannot get from a hash’s value back to its key (although you could traverse the hash table linearly looking for it, if it were unique). If you want to keep track of both a hash key and value, you need to do it yourself. Some common approaches are:

    # iterate through the table by key
    foreach my $key (keys %hash)
    {
         # here we have both the key and its corresponding value
         print "value at key $key is $hash{$key}\n";
    }
    
    # iterate through the table by keys and values
    while (my ($key, $value) = each %hash)
    {
         print "value at key $key is $value, which is the same as $hash{$key}\n";
    }
    

    Please read up on how hashes work in the manual. You can also read about the keys and each functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a C++ class with an attribute that is a reference: class
As in the title: How can I access the URL hash/fragment (the part following
how to get the value of an element in xslt. suppose my xml like
I suppose my problem is pretty obvious, but I don't know Lisp and can't
Suppose my program creates a large array of data which I then save with
Suppose my solution have two project: myApp: silverlight application project: the default application App
Suppose my attempt to write a pickle object out to disk is incomplete due
Suppose my terminal screen is 40 lines high. Suppose I type in clear; Suppose
Suppose my model is this: class Ego(models.Model): event = models.ForeignKey(Event) user = models.ForeignKey(User) As
Suppose my repository class looks like this: class myRepository : IDisposable{ private DataContext _context;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.