G'day glenn,

Perhaps something like this. It carries the $tag and $type keys through the sorting process; this allows the %licensehash data to be recreated in the sort order you want. If this isn't what you want, please show expected output.

#!/usr/bin/env perl -l use strict; use warnings; no strict 'vars'; our %licensehash = ( CI => { CI_CTAG => ['','',"", "Customer Name", 1, '[^0-9a-zA-Z\-]', [@ +new_license_versions]], CI_CID => ['','',"", "Chassis Serial", 1, '[^0-9a-zA-Z]', [@ne +w_license_versions]], CI_UID1 => ['','',"", "MAC 1/UUID", 0, '[^0-9a-zA-Z:-]', [$iTX +_2_7_VERSION, $iTX_SBB_VERSION_val]], CI_UID2 => ['','',"", "MAC 2", 0, '[^0-9a-zA-Z:-]', [$iTX_2_7_ +VERSION]], CI_SER => ['','',"", "Lenovo Serial", 0, '[^0-9a-zA-Z]', [$iTX +_Lenovo_VERSION_val]], CI_UUID => ['','',"", "Lenovo UUID", 0, '[^0-9a-zA-Z]', [$iTX_ +Lenovo_VERSION_val]], CI_E => ['','',"0", "Epoc", 0, '[^0-9]', [@new_license_version +s]], #0 unlicensed, 1 first license }, COM => { COM_PV => ['','',$iTX_SBB_VERSION_val, "iTX Version", 1, '[^0- +9.]', [@new_license_versions]], COM_MV => ['','',$VERSION, "LMU Version", 0, '[^0-9.]', [@new_ +license_versions]], COM_KT => ['','',"MASTER", "Key Type", 0, 'MASTER', [@new_lice +nse_versions]], COM_CD => ['','',sub{sprintf '%04d-%02d-%02d', $_[5]+1900, $_[ +4]+1, $_[3]}->(localtime), "Creation Date", 0, '[^0-9\-]', [@new_lice +nse_versions]], COM_USER => ['','',sub{qx"echo %username%"}, "Creation User", +0, '[^0-9a-zA-Z\-]', [@new_license_versions]], }, ISCSI => { ISCSI_T => ['','',"-1", "iSCSI", 1, "-1,89478485", [@new_licen +se_versions]], }, NAS => { NAS_T => ['','',"0", "NAS", 1, "-1,89478485", [$iTX_Lenovo_VER +SION_val, $iTX_2_7_VERSION]], }, RPL => { RPL_T => ['','',"-1", "Sync Replication", 1, "-1,89478485", [@ +new_license_versions]], }, ); use strict 'vars'; my @to_sort; foreach my $tag (keys %licensehash) { foreach my $type (keys %{$licensehash{$tag}}) { push @to_sort, [$tag, $type, $licensehash{$tag}{$type}[3]]; } } my @sorted = sort { $a->[2] cmp $b->[2] } @to_sort; use Data::Dump; for (@sorted) { print "Sort value: $_->[2]; Tag key: $_->[0]; Type key: $_->[1]"; dd $licensehash{$_->[0]}{$_->[1]}; }

Output:

Sort value: Chassis Serial; Tag key: CI; Type key: CI_CID ["", "", "", "Chassis Serial", 1, "[^0-9a-zA-Z]", []] Sort value: Creation Date; Tag key: COM; Type key: COM_CD ["", "", "2014-03-25", "Creation Date", 0, "[^0-9\\-]", []] Sort value: Creation User; Tag key: COM; Type key: COM_USER ["", "", sub { ... }, "Creation User", 0, "[^0-9a-zA-Z\\-]", []] Sort value: Customer Name; Tag key: CI; Type key: CI_CTAG ["", "", "", "Customer Name", 1, "[^0-9a-zA-Z\\-]", []] Sort value: Epoc; Tag key: CI; Type key: CI_E ["", "", 0, "Epoc", 0, "[^0-9]", []] Sort value: Key Type; Tag key: COM; Type key: COM_KT ["", "", "MASTER", "Key Type", 0, "MASTER", []] Sort value: LMU Version; Tag key: COM; Type key: COM_MV ["", "", undef, "LMU Version", 0, "[^0-9.]", []] Sort value: Lenovo Serial; Tag key: CI; Type key: CI_SER ["", "", "", "Lenovo Serial", 0, "[^0-9a-zA-Z]", [undef]] Sort value: Lenovo UUID; Tag key: CI; Type key: CI_UUID ["", "", "", "Lenovo UUID", 0, "[^0-9a-zA-Z]", [undef]] Sort value: MAC 1/UUID; Tag key: CI; Type key: CI_UID1 ["", "", "", "MAC 1/UUID", 0, "[^0-9a-zA-Z:-]", [undef, undef]] Sort value: MAC 2; Tag key: CI; Type key: CI_UID2 ["", "", "", "MAC 2", 0, "[^0-9a-zA-Z:-]", [undef]] Sort value: NAS; Tag key: NAS; Type key: NAS_T ["", "", 0, "NAS", 1, "-1,89478485", [undef, undef]] Sort value: Sync Replication; Tag key: RPL; Type key: RPL_T ["", "", -1, "Sync Replication", 1, "-1,89478485", []] Sort value: iSCSI; Tag key: ISCSI; Type key: ISCSI_T ["", "", -1, "iSCSI", 1, "-1,89478485", []] Sort value: iTX Version; Tag key: COM; Type key: COM_PV ["", "", undef, "iTX Version", 1, "[^0-9.]", []]

-- Ken


In reply to Re: sort hash by value by kcott
in thread sort hash by value by glenn

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.