in reply to Hash creation problem

Others have given you good suggestions already. I just wanted to show that you could use the Text::xSV::Slurp module to create the HoH structure for you. The whitespace separated data would be a problem for this module, so you would need your input data to be in a delimited format for this approach to work (like csv shown below). BrowserUK has shown how to deal with the whitespace separated data that you have. I would encourage you to become very familiar with how to use hashes in perl before resorting to a module to do the work for you, since they are such an important feature of perl.

This code:

#!/usr/bin/env perl use strict; use warnings; use Text::xSV::Slurp; my $hoh = xsv_slurp( \*DATA, shape => 'hoh', key => 'Subsystem', ); use Data::Dumper; print Dumper $hoh; exit; __DATA__ Subsystem,Group,PID,Status inetd,tcpip,2424886,active xntpd,tcpip,3473550,active rwhod,tcpip,,inoperative snmpd,tcpip,,inoperative aixmibd,tcpip,,inoperative hostmibd,tcpip,,inoperative snmpmibd,tcpip,,inoperative

Gives the output:

$VAR1 = { 'inetd' => { 'Group' => 'tcpip', 'Status' => 'active', 'PID' => '2424886' }, 'hostmibd' => { 'Group' => 'tcpip', 'Status' => 'inoperative', 'PID' => '' }, 'snmpmibd' => { 'Status' => 'inoperative', 'PID' => '', 'Group' => 'tcpip' }, 'aixmibd' => { 'PID' => '', 'Status' => 'inoperative', 'Group' => 'tcpip' }, 'snmpd' => { 'Group' => 'tcpip', 'Status' => 'inoperative', 'PID' => '' }, 'xntpd' => { 'Group' => 'tcpip', 'Status' => 'active', 'PID' => '3473550' }, 'rwhod' => { 'Group' => 'tcpip', 'Status' => 'inoperative', 'PID' => '' } };

Replies are listed 'Best First'.
Re^2: Hash creation problem
by BrowserUk (Patriarch) on Feb 19, 2014 at 16:20 UTC

    I would encourage you to now write the program that will take the OP's data in the form he has it and then convert it to the form that your "solution" requires it to be in to work.

    And then see how much time you saved using a module that solved a different problem.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I just wanted to show an alternative. I think the solution you posted is the most useful for the specific question at hand. Perhaps the OP has some control over the format of the input data. What if they had the means to quickly obtain the input data in delimited format (e.g. a command line switch)? Then maybe using Text::xSV::Slurp makes sense. Also, the module has additional features that may prove useful for the OP in the future.
        Thanks it's an interesting sidebar but in this instance I have no control over the data. The data is returned in this fixed format by a core operating system command.