Re: Array and Hash able manipulation



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



.



Relevant Pages

  • Re: parse hash by array element seems to run slow
    ... > I've been through the faq's and every web site that focuses on perl ... Forget about the array. ... Fill this hash first and then use a while loop on the first file. ... foreach loop over keys(%hash). ...
    (comp.lang.perl.misc)
  • RE: Create list of variables to be printed?
    ... >> I'm a perl newbie working on a script to log data from a device that ... >> means I need to edit the script any time I need to change the items ... > regex against the keys to get what you want to print out or bypass. ... I guess I could create a hash with just the variables I ...
    (perl.beginners)
  • Re: Difficulty manipulating hashes
    ... perl wrote: ... I'm thinking that a hash literal might be the spoken ... version of something else, probably with more words, in the literature, ... are used for the keys, and the even-numbered ones for the values ...
    (comp.lang.perl.misc)
  • Re: RE : Is it possible to force a particular order in a hash?
    ... Perl comes up with its own way of ... > the order in which the hash elements were added". ... IxHash elements by supplying a list of keys in the desired order. ...
    (perl.beginners)
  • Re: XML::Simple
    ... to get the XML file read in as an array instead of into a hash. ... Perl Script: ...
    (comp.lang.perl.misc)