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;