title: Question on Hash Sort and Print in Perl
body: <p>I've got trouble on how to print my hash contents. </p>
  
  <p>Code snippet as this,</p>
  
  <pre><code>#!/usr/bin/perl -w
  use strict;
  use warnings;
  
  my (%data, $keyword);
  
  while (my $line = &lt;DATA&gt;){
      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 =&gt; $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
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>D:\learning\perl&gt;sc4.pl
  Keyword3 =&gt; ARRAY(0x18418fc)
  Keyword1 =&gt; ARRAY(0x28925c)
  Keyword2 =&gt; ARRAY(0x2892fc)
  Keyword4 =&gt; ARRAY(0x184360c)
  </code></pre>
  
  <p>Actually, I don't think the Keyword1 value (<code>data1 a</code>, <em>Only one line</em>)is ARRAY. But the output still showed it was an array. </p>
  
  <p>Could you give me some suggestions on how to print it correctly.</p>
  
  <p>Appreciated for your input.</p>
  
  <p><strong>[update]</strong></p>
  
  <p>I update my code for <code>while loop</code> to try to print the values array. but still failed.</p>
  
  <pre><code>while ( my ($k,@v) = each %data ) {
      print "$k\n";
      foreach (@v) {
      print Dumper (@v);
      print "$_\n";
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>D:\learning\perl&gt;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)
  </code></pre>
  
  <p>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? </p>