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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:19:50+00:00 2026-05-27T18:19:50+00:00

I have the following code use strict; use warnings; use Data::Dumper; my $s =

  • 0

I have the following code

use strict;
use warnings;
use Data::Dumper;

my $s = "12   A    P1  
23   B    P5
24   C    P2
15   D    P1
06   E    P5 ";
my $hash = {};
my @a  = split(/\n/, $s);

foreach (@a)
{
  my $c = (split)[2];
  my $d = (split)[1];
  my $e = (split)[0];
  push(@{$hash->{$c}}, $d);
}
print Dumper($hash );

I am getting the output

    $VAR1 = {
   'P5' => [
             'B',
             'E'
           ],
   'P2' => [
             'C'
           ],
   'P1' => [
             'A',
             'D'
           ]
    };

But I want the output like

 $VAR1 = {
'P5' => {
 'E' => '06',
 'B' => '23'
  },
 'P2' => {
   'C' => '24'
 },
 'P1' => {
   'A' => '12',
   'D' => '15'
 }
  };

Please help.

  • 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-27T18:19:51+00:00Added an answer on May 27, 2026 at 6:19 pm

    You need to use a hash if you want a hash as output.

    No need to split three times and use postscripts, just split once and assign all variables. Also no need to initialize a scalar as an empty hash, perl will take care of that for you.

    I renamed the variables for increased readability.

    my $string = "12   A    P1  
    23   B    P5
    24   C    P2
    15   D    P1
    06   E    P5 ";
    my $hash;
    my @lines = split(/\n/, $string);
    
    foreach (@lines)
    {
        my ($value, $key2, $key) = split;
        $hash->{$key}{$key2} = $value;
    }
    print Dumper($hash );
    

    Be aware that if you have multiple values with the same keys, they will overwrite each other. In that case, you’d need to push the values onto an array instead:

    push @{$hash->{$key}{$key2}}, $value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following test code use Data::Dumper; my $hash = { foo =>
I have the following perl code : use strict; use warnings; use Test::Cmd::Common; my
I have a question with the following Code: #!/usr/bin/perl use strict; use warnings; my
I have the following sample code #!/usr/bin/perl use strict; use warnings; my $b =
I have the following code I use to insert form data into a single
Here's the code I have: use strict; use warnings; use Log::Log4perl qw(:easy); Log::Log4perl->init({ level
OK, I have the following code: use strict; my @ar = (1, 2, 3);
I have following code snippet that i use to compile class at the run
I have the following code which I use to match fancybox possible elements: $('a.grouped_elements').each(function(){
I'm trying to use opengl in C#. I have following code which fails with

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.