Hi Everyone, I'm parsing XML data using xml:libxml and am trying to determine the last category based on "category_id" and "parent_id" but can't figure out the logic to do it through hashes or arrays. The XML looks like this where the last category should be "Carrot"

Hi

So why should carrot be last?

No xml is required for that logic ;)

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; use constant +{ qw{ CAT_ID 0 NAME 1 PARENT_ID 2 } }; Main( @ARGV ); exit( 0 ); sub last_category(;+) { ( sort { $a->[CAT_ID] <=> $b->[CAT_ID] } @{ $_[0] } )[ -1 ] } sub Main { my @cats = ( [443120, "Vegetables", 443026], [445720, "Carrot", 443120], [443026, "Food", undef ], ); dd \@cats; dd( last_category( \@cats ) ); @cats = sort { $a->[CAT_ID] <=> $b->[CAT_ID] } @cats; dd\@cats; } __END__ [ [443120, "Vegetables", 443026], [445720, "Carrot", 443120], [443026, "Food", undef], ] [445720, "Carrot", 443120] [ [443026, "Food", undef], [443120, "Vegetables", 443026], [445720, "Carrot", 443120], ]

Although once you know the goal ... xsh probably implements a max/min function

Yup https://xsh.sourceforge.io/doc2/html/index.html#max_function

$ xsh -q xpath-max-min-11117492-correct.xsh <category_id>443120 </category_id> <category_id>445720 </category_id> <category_id>443026 </category_id> 443120 445720 443026 max: 445720 min: 443026

$ cat xpath-max-min-11117492-correct.xsh open "xpath-max-min-11117492.xml"; ls //category_id; ls //category_id/node(); echo max: xsh:max( //category_id ); echo min: xsh:min( //category_id );

In reply to Re: Hash or Array - Logic Help by Anonymous Monk
in thread Hash or Array - Logic Help by audioboxer

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.