in reply to Hash or Array - Logic Help

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 );

Replies are listed 'Best First'.
Re^2: Hash or Array - Logic Help
by choroba (Cardinal) on May 30, 2020 at 11:26 UTC
    Note that the sourceforge repo and documentation are no longer up to date. You can find the recent changes at GitHub.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Where is the page I linked?