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 7848393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:11:08+00:00 2026-06-02T18:11:08+00:00

I have a situation where I have 3 different values for each key. I

  • 0

I have a situation where I have 3 different values for each key. I have to print the data like this:

K1 V1 V2 V3
K2 V1 V2 V3
…
Kn V1 V2 V3

Is there any alternate efficient & easier way to achieve this other that that listed below? I am thinking of 2 approaches:

  1. Maintain 3 hashes for 3 different values for each key.
    Iterate through one hash based on the key and get the values from other 2 hashes
    and print it.

    Hash 1 - K1-->V1 ...
    Hash 2 - K1-->V2 ...
    Hash 3 - K1-->V3 ...
  2. Maintain a single hash with key to reference to array of values.
    Here I need to iterate and read only 1 hash.

    K1 --> Ref{V1,V2,V3}

EDIT1:

The main challenge is that, the values V1, V2, V3 are derived at different places and cannot be pushed together as the array. So if I make the hash value as a reference to array, I have to dereference it every time I want to add the next value.

E.g., I am in subroutine1 – I populated Hash1 – K1-->[V1]
I am in subroutine2 – I have to de-reference [V1], then push V2. So now the hash
becomes K1-->[V1 V2], V3 is added in another routine. K1-->[V1 V2 V3]

EDIT2:

Now I am facing another challenge. I have to sort the hash based on the V3.
Still is it feasible to store the hash with key and list reference?

K1-->[V1 V2 V3]

  • 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-06-02T18:11:10+00:00Added an answer on June 2, 2026 at 6:11 pm

    It really depends on what you want to do with your data, although I can’t imagine your option 1 being convenient for anything.

    Use a hash of arrays if you are happy referring to your V1, V2, V3 using indexes 0, 1, 2 or if you never really want to handle their values separately.

    my %data;
    $data{K1}[0] = V1;
    $data{K1}[1] = V2;
    $data{K1}[2] = V3;
    

    or, of course

    $data{K1} = [V1, V2, V3];
    

    As an additional option, if your values mean something nameable you could use a hash of hashes, so

    my %data;
    $data{K1}{name} = V1;
    $data{K1}{age} = V2;
    $data{K1}{height} = V3;
    

    or

    $data{K1}{qw/ name age height /} = (V1, V2, V3);
    

    Finally, if you never need access to the individual values, it would be fine to leave them as they are in the file, like this

    my %data;
    $data{K1} = "V1 V2 V3";
    

    But as I said, the internal storage is mostly dependent on how you want to access your data, and you haven’t told us about that.


    Edit

    Now that you say

    The main challenge is that, the values V1, V2, V3 are derived at
    different places and cannot be pushed together as the array

    I think perhaps the hash of hashes is more appropriate, but I wouldn’t worry at all about dereferencing as it is an insignificant operation as far as execution time is concerned. But I wouldn’t use push as that restricts you to adding the data in the correct order.

    Depending which you prefer, you have the alternatives of

    $data{K1}[2] = V3;
    

    or

    $data{K1}{height} = V3;
    

    and clearly the latter is more readable.


    Edit 2

    As requested, to sort a hash of hashes by the third value (height in my example) you would write

    use strict;
    use warnings;
    
    my %data = (
      K1 => { name => 'ABC', age => 99, height => 64 },
      K2 => { name => 'DEF', age => 12, height => 32 },
      K3 => { name => 'GHI', age => 56, height => 9 },
    );
    
    for (sort { $data{$a}{height} <=> $data{$b}{height} } keys %data) {
      printf "%s => %s %s %s\n", $_, @{$data{$_}}{qw/ name age height / };
    }
    

    or, if the data was stored as a hash of arrays

    use strict;
    use warnings;
    
    my %data = (
      K1 => [ 'ABC', 99, 64 ],
      K2 => [ 'DEF', 12, 32 ],
      K3 => [ 'GHI', 56, 9 ],
    );
    
    for (sort { $data{$a}[2] <=> $data{$b}[2] } keys %data) {
      printf "%s => %s %s %s\n", $_, @{$data{$_}};
    }
    

    The output for both scripts is identical

    K3 => GHI 56 9
    K2 => DEF 12 32
    K1 => ABC 99 64
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my situation. I have a graph that has different sets of data being
i have situation like this: class IData { virtual void get() = 0; virtual
In a situation where a variable could have two different values, and you do
I have a situation where different threads populate a queue (producers) and one consumer
Hi I have a situation where my application can be used for different purposes
I have seen a similar related post but my situation is a little different
I have a bit of a situation. I've consumed about fourty-eleven different tutorials/books/videos on
I have a div which has some text, but according to different situations this
I have two vector objects that contain different types of data that are ordered
I am quite ashamed to ask this, but recently there has been a situation

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.