Esteemed monks,

I'm trying new grounds, and decided I should really start using XML. Ok, so I read XML::Simple, and it really does seem simple enough. Until I ran into the following problem:

I have the following XML:

<config> <gallery> <template>admin.tmpl</template> <directory>..</directory> </gallery> <pictures> <picture> <Mtime>1039101693</Mtime> <Size>4096</Size> <Type>File</Type> <Name>picture.jpg</Name> </picture> <picture> <Mtime>1039036371</Mtime> <Size>38633</Size> <Type>File</Type> <Name>test.jpg</Name> </picture> ...

Now say I want to find the Mtime of 'test.jpg'. In the hashref returned by XML::Simple, it would be here:

${$ref->{pictures}->{picture}}[1]->{Mtime}

(or something similar). But that means that to get the information I have to know it's position in the array. Is there a way to find that position without looping over the whole array? I could defenitly do something like this:

$xmlref = grep { $_->{Name} eq 'test.jpg' } @{$ref->{pictures}->{pic +ture}};

But that seems wasteful. Is there any other way?

Thanks!

-- Dan


In reply to (z) Finding an XML element with XML::Simple by zigdon

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.