Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You can break out the record fields by processing the record string as a list of lines:

use strict; use warnings; use Data::Dumper; my %records; local $/ = "\n\n"; while (<DATA>) { next if !/^(\d+):(.*)/s; my ($id, $tail) = ($1, $2); local $/ = "\n"; open my $recIn, '<', \$tail; while (<$recIn>) { chomp; #next if !/(\w+)\s*=\s*(.*)/; # #$records{$id}{$1} = $2; next if !/^\s*?([^=]+)\s*=\s*(.*)/; my ($key, $value) = ($1, $2); s/^\s+|\s+$//g for $key, $value; $records{$id}{$key} = $value; } } print Dumper(\%records); __DATA__ ...

Using the previous data prints:

$VAR1 = { '1' => { 'Capacity' => '288196762624 (268.4G)', 'WWN' => '06:00:00:00:05:00:00:00:00:00:00:00:00:00 +:00:03', 'Pool' => 'performance', 'Model' => 'STE30065 CLAR300', 'Maximum speed' => '6 Gbps', 'Health details' => '"The component is operating no +rmally. No action is required."', 'Vendor capacity' => '322122547200 (300.0G)', 'Part number' => '005049273', 'Enclosure' => 'DPE_0', 'Health state' => 'OK (5)', 'Serial number' => '6SJ2C6MV', 'Slot' => '0', 'Type' => 'SAS', 'User capacity' => '236420176896 (220.2G)', 'ID' => 'disk_dpe_0_0', 'Manufacturer' => 'SEAGATE', 'Name' => 'DPE Disk 0', 'Current speed' => '6 Gbps', 'Rotational speed' => '15000 rpm', 'Firmware revision' => 'ES0E' }, '2' => { 'WWN' => '06:00:00:00:05:00:00:00:01:00:00:00:01:00 +:00:03', 'Pool' => 'performance', 'Capacity' => '288196762624 (268.4G)', 'Slot' => '1', 'Serial number' => '6SJ28QF3', 'Health state' => 'OK (5)', 'Part number' => '005049273', 'Enclosure' => 'DPE_0', 'Model' => 'STE30065 CLAR300', 'Maximum speed' => '6 Gbps', 'Health details' => '"The component is operating no +rmally. No action is required."', 'Vendor capacity' => '322122547200 (300.0G)', 'ID' => 'disk_dpe_0_1', 'User capacity' => '236420176896 (220.2G)', 'Manufacturer' => 'SEAGATE', 'Type' => 'SAS', 'Rotational speed' => '15000 rpm', 'Firmware revision' => 'ES0E', 'Current speed' => '6 Gbps', 'Name' => 'DPE Disk 1' } };

Note that local overrides the variable's value just for the current block so changing $/ inside the main loop doesn't affect while (<DATA>). open my $recIn, '<', \$tail; treats $tail as a file.

Update: Replaced commented out code to fix the single word matches for keys issue caught by tybalt89.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re^3: Unable to get the paragraph in the list of hashes. Getting single lines instead. by GrandFather
in thread Unable to get the paragraph in the list of hashes. Getting single lines instead. by pritesh_ugrankar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found