Try

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $header='u_ci_id,u_display_name,name,fqdn,ip_address,mac_address'; my $field = $ARGV[0] || 'u_ci_id'; my $input_xml = do { local $/; <DATA> }; my $twig = new XML::Twig; $twig->xparse($input_xml); my $root = $twig->root(); my @data = (); my @cols = split ',',$header; unshift @cols, $field; # added for sort for my $record ($root->children) { push @data,[ map { $record->first_child($_)->text } @cols]; } my @sorted = sort { $b->[0] cmp $a->[0] } @data; print $header."\n"; for (@sorted){ print join ',',@$_[1..$#cols]; # exclude sort col print "\n"; } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <response> <result> <fqdn>fqdnB</fqdn> <ip_address>ip1</ip_address> <u_display_name>display1</u_display_name> <name>name1</name> <mac_address>mac5</mac_address> <u_ci_id>id 1</u_ci_id> </result> <result> <fqdn>fqdnA</fqdn> <ip_address>ip2</ip_address> <u_display_name>display2</u_display_name> <name>name2</name> <mac_address>mac4</mac_address> <u_ci_id>id 2</u_ci_id> </result> </response>
poj

In reply to Re: Need help with node names for creating header line by poj
in thread Need help with node names for creating header line by vlturner

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.