Respected Monks,

This is regarding an issue I've been trying to fix the whole day, but have not succeeded.Will be thankful if you could help.

I have a text file that has the following content (command output).

Storage system address: 192.168.1.2 Storage system port: 443 HTTPS connection 1: ID = disk_dpe_0_0 Enclosure = DPE_0 Slot = 0 Name = DPE Disk 0 Health state = OK (5) Health details = "The component is operating normally. No ac +tion is required." Type = SAS Capacity = 288196762624 (268.4G) Rotational speed = 15000 rpm User capacity = 236420176896 (220.2G) Pool = performance Current speed = 6 Gbps Maximum speed = 6 Gbps Manufacturer = SEAGATE Model = STE30065 CLAR300 Vendor capacity = 322122547200 (300.0G) Part number = 005049273 Serial number = 6SJ2C6MV Firmware revision = ES0E WWN = 06:00:00:00:05:00:00:00:00:00:00:00:00:00:00 +:03 2: ID = disk_dpe_0_1 Enclosure = DPE_0 Slot = 1 Name = DPE Disk 1 Health state = OK (5) Health details = "The component is operating normally. No ac +tion is required." Type = SAS Capacity = 288196762624 (268.4G) Rotational speed = 15000 rpm User capacity = 236420176896 (220.2G) Pool = performance Current speed = 6 Gbps Maximum speed = 6 Gbps Manufacturer = SEAGATE Model = STE30065 CLAR300 Vendor capacity = 322122547200 (300.0G) Part number = 005049273 Serial number = 6SJ28QF3 Firmware revision = ES0E WWN = 06:00:00:00:05:00:00:00:01:00:00:00:01:00:00 +:03 .......................many more similar lines.

What I want to do is, create an anonymous array such that each value in the array is an anonymous hash that contains a paragraph from the text above. Paragraph meaning the content between the blank lines. What I am getting right now, is each line which becomes an anonymous hash.

######################## use strict; use warnings; use Data::Dumper; ######################## my $filename = "physical_disks.txt"; my $aoh_ref_physical_disks; open my $fh, "<", $filename or die "Cannot open file $filename: $!"; while (<$fh>) { #Do stuff here so that the following Array of Hashes is generated. #Try later to put all the seperate next ifs into a single statemen +t. next if $_=~ /^Storage/; next if $_=~ /^HTTPS/; next if $_=~ /^$/; my ($filekey, $filevalue) = (split /\s+=\s+/, $_); #print "$filekey => $filevalue\n"; push @$aoh_ref_physical_disks, {$filekey => $filevalue}; } foreach my $disk (1..$#$aoh_ref_physical_disks) { foreach my $key (sort keys $aoh_ref_physical_disks->[$disk]->%*) { #Add if loop here that checks if the key "Health status" #is anything but ok or if the key "Health details" does not #contain the word "normal", then print out the other details #such as #$aoh_ref_physical_disks->[$disk]->{'ID'} #$aoh_ref_physical_disks->[$disk]->{'Health status'} #$aoh_ref_physical_disks->[$disk]->{'Health details'} #$aoh_ref_physical_disks->[$disk]->{'Model'} } } #Use Data::Dumper output to verify. print Dumper($aoh_ref_physical_disks);

The output I get is:

C:\Users\pritesh\perlscripts>perl test.pl $VAR1 = [ { '1: ID' => 'disk_dpe_0_0 ' }, { ' Enclosure' => 'DPE_0 ' }, { ' Slot' => '0 ' }, { ' Name' => 'DPE Disk 0 ' }, { ' Health state' => 'OK (5) ' }, ]; #and so on............. C:\Users\pugrankar\perlscripts>

So basically, I am not able to tell perl that I want each hash to contain the lines from one paragraph. Kindly help me.


In reply to 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":



  • 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.