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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:10:47+00:00 2026-05-18T05:10:47+00:00

I’ve got trouble on how to print my hash contents. Code snippet as this,

  • 0

I’ve got trouble on how to print my hash contents.

Code snippet as this,

#!/usr/bin/perl -w
use strict;
use warnings;

my (%data, $keyword);

while (my $line = <DATA>){
    next unless $line =~ /\S/;
    chomp $line;
    if ($line =~ /^Keyword/){
        $keyword = $line;
    }
    else {
        push @{$data{$keyword}}, $line;
    }
}

# How to sort by keys using while loop?
while ( my ($k,$v) = each %data ) { 
    print "$k => $v\n";
}

# BTW, foreach loop sorting works.
#foreach my $key (sort (keys(%data))) {
#   print "$key \t$data{$key}\n";
#}

__DATA__
Keyword1
data1 a
Keyword2
data2 a
data2 b
data2 c
Keyword3


data3 a
data3 b


Keyword4
data4 a
data4 b

Output:

D:\learning\perl>sc4.pl
Keyword3 => ARRAY(0x18418fc)
Keyword1 => ARRAY(0x28925c)
Keyword2 => ARRAY(0x2892fc)
Keyword4 => ARRAY(0x184360c)

Actually, I don’t think the Keyword1 value (data1 a, Only one line)is ARRAY. But the output still showed it was an array.

Could you give me some suggestions on how to print it correctly.

Appreciated for your input.

[update]

I update my code for while loop to try to print the values array. but still failed.

while ( my ($k,@v) = each %data ) {
    print "$k\n";
    foreach (@v) {
    print Dumper (@v);
    print "$_\n";
    }
}

Output:

D:\learning\perl>sc4.pl
Keyword3
$VAR1 = [
          'data3 a',
          'data3 b'
        ];
ARRAY(0x189a674)
Keyword1
$VAR1 = [
          'data1 a'
        ];
ARRAY(0x28925c)
Keyword2
$VAR1 = [
          'data2 a',
          'data2 b',
          'data2 c'
        ];
ARRAY(0x2892fc)
Keyword4
$VAR1 = [
          'data4 a',
          'data4 b'
        ];
ARRAY(0x1841a74)

I already defined another foreach loop included by while loop to handle the array values. But it doesn’t work well. I don’t know why?

  • 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-18T05:10:47+00:00Added an answer on May 18, 2026 at 5:10 am

    First of all, you need to define what you mean by “sort”. Are you sorting by keys? By values?

    If you’re sorting by key, the simplest is to use Perl’s built-in sort like this:

    foreach (sort keys %data) {
        print "$_ => $data{$_};
    }
    

    If you want to sort by value you can define an anonymous sort function that sorts by value:

    foreach (sort {$data{$a} <=> $data{$b}} keys %data) {
        print "$_ => $data{$_};
    }
    

    And if you want any other sorting, you can define a function that operates on the global variables $a and $b (they are set automatically by sort) and returns -1, 0 or 1 depending the sort order.

    If you must do it with a while loop, I’ll need to give it some more thought.

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

Sidebar

Related Questions

No related questions found

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.