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;

In reply to Sorting array of hashes according to key value by rbi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.