being curious about AnoMonk's unpack suggestion, I hacked the following proof of concept.
(NB: you might need to trim the fields!)
use warnings; use strict; use Data::Dump; my %HoH; while ( my $line = <DATA> ){ my %fields; my ( $slot, @values ) = unpack( 'A3A17A16A19', $line ); @fields{'Description','HW Version','Software Version'} = @values; $HoH{$slot}= \%fields; } dd \%HoH; __DATA__ 8 CAM (0D, 12U) CAM-01122W/K05 CMTS_V07.03.01.39 112 days 2:26: +10 9 CAM (0D, 12U) CAM-01122W/K05 CMTS_V07.03.01.39 112 days 2:26: +10 10 CAM (16D, 0U) CAM-20016W/G04 CMTS_V07.03.01.39 112 days 2:29: +46 11 CAM (16D, 0U) CAM-20016W/G04 CMTS_V07.03.01.39 112 days 2:30: +15
OUTPUT:
{ " 8" => { Description => "CAM (0D, 12U)", "HW Version" => "CAM-01122W/K05", "Software Version" => "CMTS_V07.03.01.39", }, " 9" => { Description => "CAM (0D, 12U)", "HW Version" => "CAM-01122W/K05", "Software Version" => "CMTS_V07.03.01.39", }, 10 => { Description => "CAM (16D, 0U)", "HW Version" => "CAM-20016W/G04", "Software Version" => "CMTS_V07.03.01.39", }, 11 => { Description => "CAM (16D, 0U)", "HW Version" => "CAM-20016W/G04", "Software Version" => "CMTS_V07.03.01.39", }, }
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
introduced temporary @values to "ungolf" code
if you have control over the format, you should consider using a good delimiter like "\t;" ¹ instead of fixed length. Otherwise you'll need to fix code at several places each time you a field-length changes. At least the headlines should be aligned and without whitespace to help scanning field width.
¹) or multiple whitespace /\s{3,}/
In reply to Re: Defining hash structure
by LanX
in thread Defining hash structure
by mtx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |