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?
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:
If you want to sort by value you can define an anonymous sort function that sorts by value:
And if you want any other sorting, you can define a function that operates on the global variables
$aand$b(they are set automatically bysort) and returns-1,0or1depending the sort order.If you must do it with a
whileloop, I’ll need to give it some more thought.