teamassociated has asked for the wisdom of the Perl Monks concerning the following question:

I need to be able to print this data structure and cant get it to print correctly. Thanks in advance!!! from dumper:
$VAR1 = { '/cacxxx/pxxx/datxxx/' => { 'free' => '27.6', 'sz' => '200', 'mxs' => { '200' => 'GB' }, 'dfree' => '168', 'avail' => '55' }, '/cachxxx/usr/xxx/xxx/' => { ...

while (<$fh>) { next if /^spawn/i .. /^right/i; if ( /(Cxxx Database Free Space)/ ) { $Ggroup = $1; } next if /Database\s+|Max Size|Size|Available|%Free|Disk Free|facxx +xx|$uxdate.*/i; next if $. == 35 or $. == 36; next if /cacheaudit/i .. eof; $db = +(split)[0]; $mxsz = +(split)[1]; $mxsz1 = substr($mxsz, 0, -2); $mxunit = substr($mxsz, -2, 2); $sz = +(split)[2]; $sz1 = substr($sz, 0, -2); $szunit = substr($sz, -2, 2); $avail = +(split)[3]; $avail1 = substr($avail, 0, -2); $avunit = substr($avail, -2, 2); $free = +(split)[4]; $dfree = +(split)[5]; $dfree1 = substr($dfree, 0, -2); $dfunit = substr($dfree, -2, 2); $cacheDBs{$db} = { 'mxs' => {$mxsz1, $mxunit}, 'sz' => $sz1, 'avail' => $avail1, 'free' => $free, 'dfree' => $dfree1, }; for my $key (keys %cacheDBs) { print "\n/opt/freeware/bin/gmetric -n $db -T $db -g $Ggroup -t dou +ble -u MaxSize -v $cacheDBs{$db}->{mxs} -d 45)\n"; } __out__ /opt/freeware/bin/gmetric -n /cacxxx/pixxx/daxxx/ -T /cacxxx/pitxx/dax +xx/ -g Caxxx Database Free Space -t double -u MaxSize -v HASH(0x2015d +98c) -d 45)
needs to be:
/opt/freeware/bin/gmetric -n /cacxxx/pitxxx/daxxx/ -T /cacxxx/pitxxx/d +axxx/ -g Caxxx Database Free Space -t double -u MaxSize GB -v 250 -d +45)

sample data set is:
Caxxx Database Free Space Jul 09 2013 8:44 AM Database Max Size Size Available %Free + Disk Free /cacxxx/crxxx/dxxx/ 240GB 240GB 53GB 21.88 1 +33GB /cacxxx/cxxx/daxxx/ 280GB 280GB 58GB 20.79 1 +33GB

Replies are listed 'Best First'.
Re: print a hohoh
by choroba (Cardinal) on Jul 09, 2013 at 13:31 UTC
    $cacheDBs{$db}{mxs} is a hash reference. If you want to print its keys and values, do so:
    for my $key (keys %cacheDBs) { my $mxs = join ':', map "$_ $cacheDBs{$db}{mxs}{$_}", keys %{ $cac +heDBs{$db}{mxs} }; print "\n/opt/freeware/bin/gmetric -n $db -T $db -g $Ggroup -t dou +ble -u MaxSize -v $mxs -d 45)\n"; }

    Untested (the code was not runnable to demonstrate the issue).

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      ok thank you! it ran to completion, however I dont want the GB or MB printed which is why I put it as a value: any way I can only print the 1.0 in mxs and not the GB? I still need the letters GB/MB but cannot have them in the gmetric CLI. maybe use an array to store GB/MB?
      '/cache-m/usr/cachesys/mgr/' => { 'free' => '91.7', 'sz' => '1.0', 'mxs' => { '1.0' => 'GB' }, 'dfree' => '2.2', 'avail' => '917'
        Not a nice way, but if it's only for printing, treat it like a number, e.g. add 0 to it and print the result.


        I'm too lazy to be proud of being impatient.
Re: print a hash ref
by johngg (Canon) on Jul 09, 2013 at 16:53 UTC
    next if $. == 35 or $. == 36;

    Did you know that you can use the flip-flop with line numbers?

    $ perl -Mstrict -Mwarnings -E ' open my $inFH, q{<}, \ <<EOD or die $!; Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 EOD while ( <$inFH> ) { next if 3 .. 5; print; }' Line 1 Line 2 Line 6 Line 7 $

    Just another bit of Perl shorthand :-)

    Cheers,

    JohnGG

Re: print a hohoh
by Skeeve (Parson) on Jul 09, 2013 at 13:30 UTC

    Maybe format helps?


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e