Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

hash question

by dbs (Sexton)
on Aug 23, 2011 at 19:32 UTC ( #921978=perlquestion: print w/replies, xml ) Need Help??

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

Hi All! Ive been struggling with this code for too long. I need a hash that looks like aixFsMountPoint.1 = "/" => aixFsSize.1 = 3072 => aixFsFree.1 = 2631. aixFsMountPoint.6 = "/admin" => aixFsSize.6 = 128 => aixFsFree.6 = 127 ....etc.
#!/usr/bin/env perl use strict; use warnings; while (<DATA>) { next unless /^aixFsMountPoint|aixFsSize|aixFsFree/i; if ( /aixFsMount.*/i ) { push @mt, $_; } if ( /aixFsFree.*/i ) { push @free, $_ } if ( /aixFsSize.*/i ) { push @sz, $_; } } @snmpo{ @mt, @sz } = @free; print "\n",Dumper \%snmpo,"\n"; __DATA__ actual STDOUT: aixFsMountPoint.1 = "/" aixFsMountPoint.2 = "/usr" aixFsMountPoint.3 = "/var" aixFsMountPoint.4 = "/tmp" aixFsMountPoint.5 = "/local.home" aixFsMountPoint.6 = "/admin" aixFsMountPoint.7 = "/proc" aixFsMountPoint.8 = "/opt" aixFsMountPoint.9 = "/var/adm/ras/livedump" aixFsMountPoint.10 = "/net" aixFsMountPoint.11 = "/net/nim01at/opt/CBC-CFG" aixFsMountPoint.12 = "/net/nim01at/opt/CBC-CFG" aixFsSize.1 = 3072 aixFsSize.2 = 8512 aixFsSize.3 = 10240 aixFsSize.4 = 4096 aixFsSize.5 = 64 aixFsSize.6 = 128 aixFsSize.7 = 0 aixFsSize.8 = 4096 aixFsSize.9 = 3072 aixFsSize.10 = 0 aixFsSize.11 = 7168 aixFsSize.12 = 7168 aixFsFree.1 = 2631 aixFsFree.2 = 3232 aixFsFree.3 = 2373 aixFsFree.4 = 1923 aixFsFree.5 = 46 aixFsFree.6 = 127 aixFsFree.7 = 0 aixFsFree.8 = 3000 aixFsFree.9 = 3071 aixFsFree.10 = 0 aixFsFree.11 = 3463 aixFsFree.12 = 3463
Dumper outputs some undef entries. thanks!!!

Replies are listed 'Best First'.
Re: hash question
by toolic (Bishop) on Aug 23, 2011 at 19:47 UTC
    I don't understand the output you want, but it is simple to stuff it all into a hash-of-hashes data structure (perldsc). UPDATE: I mistakenly left the "next" line in there from the OP's code. It is unnecessary, and I have commented it out.
    #!/usr/bin/env perl use Data::Dumper; use strict; use warnings; my %data; while (<DATA>) { #next unless /^aixFsMountPoint|aixFsSize|aixFsFree/i; if ( /(\w+).(\d+)\s*=\s*(.*)/ ) { $data{$1}{$2} = $3; } } print Dumper(\%data); __DATA__ actual STDOUT: aixFsMountPoint.1 = "/" aixFsMountPoint.2 = "/usr" aixFsMountPoint.3 = "/var" aixFsMountPoint.4 = "/tmp" aixFsMountPoint.5 = "/local.home" aixFsMountPoint.6 = "/admin" aixFsMountPoint.7 = "/proc" aixFsMountPoint.8 = "/opt" aixFsMountPoint.9 = "/var/adm/ras/livedump" aixFsMountPoint.10 = "/net" aixFsMountPoint.11 = "/net/nim01at/opt/CBC-CFG" aixFsMountPoint.12 = "/net/nim01at/opt/CBC-CFG" aixFsSize.1 = 3072 aixFsSize.2 = 8512 aixFsSize.3 = 10240 aixFsSize.4 = 4096 aixFsSize.5 = 64 aixFsSize.6 = 128 aixFsSize.7 = 0 aixFsSize.8 = 4096 aixFsSize.9 = 3072 aixFsSize.10 = 0 aixFsSize.11 = 7168 aixFsSize.12 = 7168 aixFsFree.1 = 2631 aixFsFree.2 = 3232 aixFsFree.3 = 2373 aixFsFree.4 = 1923 aixFsFree.5 = 46 aixFsFree.6 = 127 aixFsFree.7 = 0 aixFsFree.8 = 3000 aixFsFree.9 = 3071 aixFsFree.10 = 0 aixFsFree.11 = 3463 aixFsFree.12 = 3463
    prints...
    $VAR1 = { 'aixFsFree' => { '6' => '127', '11' => '3463', '3' => '2373', '7' => '0', '9' => '3071', '12' => '3463', '2' => '3232', '8' => '3000', '1' => '2631', '4' => '1923', '10' => '0', '5' => '46' }, 'aixFsMountPoint' => { '6' => '"/admin"', '11' => '"/net/nim01at/opt/CBC-CFG"', '3' => '"/var"', '7' => '"/proc"', '9' => '"/var/adm/ras/livedump"', '12' => '"/net/nim01at/opt/CBC-CFG"', '2' => '"/usr"', '8' => '"/opt"', '1' => '"/"', '4' => '"/tmp"', '10' => '"/net"', '5' => '"/local.home"' }, 'aixFsSize' => { '6' => '128', '11' => '7168', '3' => '10240', '7' => '0', '9' => '3072', '12' => '7168', '2' => '8512', '8' => '4096', '1' => '3072', '4' => '4096', '10' => '0', '5' => '64' } };
      Wouldn't it be more careful to look for a literal "." rather than any character?

      In other words, use

      /(\w+)\.(\d+)\s*=\s*(.*)/
      rather than use
      /(\w+).(\d+)\s*=\s*(.*)/

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Wouldn't it be more careful to look for a literal "." rather than any character?
        Yes, that is what I meant to do. I forgot to back-whack the .

      Here's the same thing, but with a single regular expression pattern...

      while (<DATA>) { next unless m{ ^ ( aixFsMountPoint | aixFsSize | aixFsFree ) # $1 \. ( \d+ ) # $2 \s* = \s* ( .* ) # $3 }ix; $data{$1}{$2} = $3; }

      But there are plenty of excellent config file (INI file) modules on CPAN for doing this work.

      Also, beware the use of the i modifier to match case-insensitively tokens you're then going to use as the keys of a hash.

      aixFsMountPoint ≠ aixfsmountpoint ≠ AIXFSMOUNTPOINT

      I had attempted another try using
      while (<$SN>) { next unless /^aixFsMountPoint|aixFsSize|aixFsFree/i; my(@tmp) = split("=", $_, 0); my ($id) = $tmp[0]; my ($fs) = $tmp[1]; $snmpo{$id} = $fs; }
      But I like the regexp way better! thx!!!
Re: hash question
by CountZero (Bishop) on Aug 23, 2011 at 21:52 UTC
    I'd use a Hash of Arrayrefs.
    use Modern::Perl; use Data::Dump qw /dump/; my %hash; while (<DATA>) { chomp; my ($item, $sequence, $value) = m/(aixFsMountPoint|aixFsSize|aixFs +Free)\.(\d+)\s=\s"?([^"]*)/; next unless $item; $hash{$item}[$sequence] = $value; } say dump(\%hash); __DATA__ actual STDOUT: aixFsMountPoint.1 = "/" aixFsMountPoint.2 = "/usr" aixFsMountPoint.3 = "/var" aixFsMountPoint.4 = "/tmp" aixFsMountPoint.5 = "/local.home" aixFsMountPoint.6 = "/admin" aixFsMountPoint.7 = "/proc" aixFsMountPoint.8 = "/opt" aixFsMountPoint.9 = "/var/adm/ras/livedump" aixFsMountPoint.10 = "/net" aixFsMountPoint.11 = "/net/nim01at/opt/CBC-CFG" aixFsMountPoint.12 = "/net/nim01at/opt/CBC-CFG" aixFsSize.1 = 3072 aixFsSize.2 = 8512 aixFsSize.3 = 10240 aixFsSize.4 = 4096 aixFsSize.5 = 64 aixFsSize.6 = 128 aixFsSize.7 = 0 aixFsSize.8 = 4096 aixFsSize.9 = 3072 aixFsSize.10 = 0 aixFsSize.11 = 7168 aixFsSize.12 = 7168 aixFsFree.1 = 2631 aixFsFree.2 = 3232 aixFsFree.3 = 2373 aixFsFree.4 = 1923 aixFsFree.5 = 46 aixFsFree.6 = 127 aixFsFree.7 = 0 aixFsFree.8 = 3000 aixFsFree.9 = 3071 aixFsFree.10 = 0 aixFsFree.11 = 3463 aixFsFree.12 = 3463
    Output
    { aixFsFree => [undef, 2631, 3232, 2373, 1923, 46, 127, 0, 3000, + 3071, 0, 3463, 3463], aixFsMountPoint => [ undef, "/", "/usr", "/var", "/tmp", "/local.home", "/admin", "/proc", "/opt", "/var/adm/ras/livedump", "/net", "/net/nim01at/opt/CBC-CFG", "/net/nim01at/opt/CBC-CFG", ], aixFsSize => [undef, 3072, 8512, 10240, 4096, 64, 128, 0, 4096 +, 3072, 0, 7168, 7168], }
    Of course, whether such datastructure is useful or not will ultimately depend on what you want to do with the data.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: hash question
by roboticus (Chancellor) on Aug 23, 2011 at 19:36 UTC

    dbs:

    Not with the code you've shown, it doesn't. It won't even compile. Please make a small example that shows the problem.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://921978]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2021-04-23 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?