Re: Array and Hash able manipulation
- From: Brian <googleAcct@xxxxxxxxxxxxxxx>
- Date: 17 Apr 2007 09:33:34 -0700
On Apr 17, 6:55 am, maxwe...@xxxxxxxxx wrote:
Dear Friends,
I have been trying for days to deal with this problem, any help would
be greatly appreciated.
Having sent it an XML file, I get this result from XML::Simple::XMLin
$VAR1 = {
'vinyl' => [
{
'artist' => 'Deep Purple',
'label' => 'EMI',
'title' => 'Machine Head',
'year' => '1971'
},
{
'artist' => 'Bitches Brew',
'label' => 'Columbia',
'title' => 'Miles Davis',
'year' => '1970'
}
]
};
What I would like to do with this information is to print out the key
which in this case is "vinyl", but will not necessarily be "vinyl" the
next time I run the script. I would then like to run a loop for each
element in the hash table this element contains, printing out the
names of the keys (whatever they are) and the values.
The result would therefore be:
vinyl
artist: Deep Purple
label: EMI
title: Machine Head
year: 1971
The same algorithm would also be able to print out the following,
given a different source XML file:
minidisc
cuplrit: Weird Al Yankovic
henchmen: Scotti Brothers
title: Polka Party!
year: 1986
I have tried a whole bunch of different ways to access this
information, including @temp. I can get to the values with
foreach my $elem (@{$stream->{vinyl}}) {
print $elem->{title}."<br>";
}
However, I need to be able to print the names of the keys out without
knowing what they are called.
This is a real chance to study.. I mean it. After many, many years of
being a C and C++ wizard, I have been brought to a crawl by Perl and
XML. Just different enough so that I can't "just type it" and similar
enough so I feel like I should know how to do it already!
What I did was take a series of XML files, run them through
XML::Simple, and write the expressions that retrieve various elements.
The output of XML::Simple is challenging enough so that a few times I
thought to throw in the towel and just go back to C. But when I went
through a similar exercise with my handy IDE/Debugger, I realized that
for all the clarity and familiarity, one wrong pointer and crash-ola.
That and the remote, server'ness of Perl made me come back.
And like the German I took in school, each time I work through it, it
is hard all over again.
But I I am getting the hang of it finally. The Hash refs and Array
refs are an obstacle course. This should be really basic, and its
actually just hard enough to be not really basic.
As for looping through an array, and keys %hash, those are idiomatic,
and have to be learned.
the online docs and docs with your perl install are just ok, but do
have a lot of information. rtfm
-Brian
.
- References:
- Array and Hash able manipulation
- From: maxwells
- Array and Hash able manipulation
- Prev by Date: Array and Hash able manipulation
- Next by Date: Re: Array and Hash able manipulation
- Previous by thread: Array and Hash able manipulation
- Next by thread: Re: Array and Hash able manipulation
- Index(es):
Relevant Pages
|