Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Sorting array of hashes according to key value

by rbi (Monk)
on Nov 28, 2003 at 19:15 UTC ( #310767=perlquestion: print w/ replies, xml ) Need Help??
rbi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a list of items that can be described like this in an XML file:
#dd.xml is a flie like this: #<opt> # <Item> # <User>a</User> # <Date>2003-11-27 18:00</Date> # <Title>Title </Title> # <Link>Link a 1</Link> # <Link>Link a 2</Link> # <IsVisible>1</IsVisible> # </Item> # <Item> # <User>b</User> ## <Date>2003-11-27 19:00</Date> # <Title>Title b</Title> # <Link>Link b</Link> # <IsVisible>0</IsVisible> # </Item> # <Item> # <User>b</User> ## <Date>2003-11-27 18:00</Date> # <Title>Title b</Title> # <Link>Link c</Link> # <IsVisible>0</IsVisible> # </Item> #</opt>
I am trying to load these items and then print them according to their date.
I came up with this code, that seems to work in preparing an array of hashes, but then I'm unable to make a "smart" sort in order to print the hashes according to the Date key value.
Thanks for any hint/correction.
R.
use strict; my $list = new List; my $file = './dd.xml'; $list->set_items($file); $list->show(); package List; use strict; use XML::Simple; use Data::Dumper; sub new { my $class = shift; my $self = {}; bless($self, $class); $self->{Items} = (); return $self; } sub set_items { my $self = shift(); my $file = shift(); # Here I load the xml file my %tmp = %{XMLin($file,noattr=>1)}; $self->{Items} = $tmp{Item}; # my $d = Dumper(\$self); # print "$d\n"; } sub show { my $self = shift(); my $q = shift(); # Here I build the array of hashes my @items = @{$self->{Items}}; # my $d = Dumper(\@items); # print "$d\n"; my $i; for $i (0..$#items) { print "-------------\n"; my %hash = %{$items[$i]}; my $d = Dumper(\%hash); print "$d\n"; # my $date = $hash{Date}; } } 1;

Comment on Sorting array of hashes according to key value
Select or Download Code
Re: Sorting array of hashes according to key value
by diotalevi (Canon) on Nov 28, 2003 at 19:26 UTC
    @{$self->{'Items'}} = sort { $a->{'Date'} cmp $b->{'Date'} } @{$self->{'Items'}};
Re: Sorting array of hashes according to key value
by ysth (Canon) on Nov 28, 2003 at 19:30 UTC
    Since I don't see an attempt at a sort statement in that code, its hard to guess where you want the sort. To sort a list of hash references by one of the values, you would say sort { $a->{Date} cmp $b->{Date} }, adjusting the particular key used and perhaps changing cmp to <=> to suit.

    (Update: was showing array refs, change to hash refs)

    On another topic, where you have:

    my $i; for $i (0..$#items) { print "-------------\n"; my %hash = %{$items[$i]}; my $d = Dumper(\%hash); print "$d\n"; # my $date = $hash{Date}; }
    I have a suggestion. Instead of looping over the indices, it is more natural in Perl to loop over the array itself:
    for my $hashref (@items) { my $d = Dumper($hashref); print "$d\n"; # my $date = $hashref->{Date}; }
Re: Sorting array of hashes according to key value
by NetWallah (Abbot) on Nov 28, 2003 at 20:33 UTC
    Since the SORT issue is resolved by previous commentators - here is my $0.02 towards perl style and object design:

    First ++ ysth on making the "perlish" loop. Here is a way to get rid of the extra array copy/intermediate/unnecessary variable " @items" for the same code segment:

    for my $hashref (@{$self->{Items}}) { my $d = Dumper($hashref); print "$d\n"; # my $date = $hashref->{Date}; }
    AN additional option is that your "new" method could allow for early loading of the structure if it replaced "$self->{Items} = ();" with the following lines:
    my $file = shift(); if ($file){ $self->set_items($file); }else{ $self->{Items} = (); }
    This allows you the option of calling the "new" function like this:
    my $list = new List($file);
      Thanks a lot for all the indications.
      You got I'm trying to learn objects as well... :) R.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://310767]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-12-19 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (70 votes), past polls