Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Defining hash structure

by mtx (Initiate)
on Aug 25, 2014 at 08:58 UTC ( #1098506=perlquestion: print w/ replies, xml ) Need Help??
mtx has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys, im a newbie at Perl, my apologies if I ask something that is weird. Im having a trouble here for an exam, it requires me to use this input:
Slot Description HW Version Software Version Uptime 0 CAM (0D, 12U) CAM-01122W/K04 CMTS_V07.03.01.39 112 days 2:26: +09 1 CAM (0D, 12U) CAM-01122W/K04 CMTS_V07.03.01.39 112 days 2:26: +09 2 CAM (0D, 12U) CAM-01122W/K04 CMTS_V07.03.01.39 112 days 2:26: +09 3 CAM (0D, 12U) CAM-01122W/K04 CMTS_V07.03.01.39 112 days 2:26: +10 4 CAM (0D, 12U) CAM-01122W/K05 CMTS_V07.03.01.39 112 days 2:26: +10 5 CAM (0D, 12U) CAM-01122W/K04 CMTS_V07.03.01.39 112 days 2:26: +09 6 CAM (0D, 12U) CAM-01122W/K04 CMTS_V07.03.01.39 112 days 2:26: +10 Press <space> to continue, <cr> to single step, ? for help 7 CAM (0D, 12U) CAM-01122W/K05 CMTS_V07.03.01.39 112 days 2:26: +09 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 12 CAM (16D, 0U) CAM-20016W/G04 CMTS_V07.03.01.39 112 days 2:29: +51 13 CAM (16D, 0U) CAM-20016W/G04 CMTS_V07.03.01.39 112 days 2:29: +33 14 CAM (16D, 0U) CAM-20016W/G04 CMTS_V07.03.01.39 112 days 2:29: +47 15 CAM (16D, 0U) CAM-20016W/G04 CMTS_V07.03.01.39 112 days 2:29: +52 Press <space> to continue, <cr> to single step, ? for help 17 RCM A RCM-01000W/E04 CMTS_V07.03.01.39 112 days 2:29: +24 18 RCM B RCM-01000W/E04 CMTS_V07.03.01.39 112 days 2:28: +22 19 SCM A SCM-02440W/B10 CMTS_V07.03.01.39 112 days 2:32: +51 20 SCM B SCM-02440W/B10 CMTS_V07.03.01.39 112 days 2:32: +51
and output the following structure:
my %slotHash = ( "9" = ( type => "CAM", #The first three characters of the descr +iption slotID => 9, description => "CAM (0D, 12U)", hwVersion => "CAM-01122W/K05", swVersion => "CMTS_V07.03.01.39", uptime => 9685570 ), "17" = ( type => "RCM", #The first three characters of the descr +iption slotID => 17, description => "RCM A", hwVersion => "RCM-01000W/E04", swVersion => "CMTS_V07.03.01.39", uptime => 9685764 ), );

Can you guys point me in the right direction?

Im having problems how to declare a hash like this, this input is weird, maybe I can change the input to a delimiter or something, if I do this, how can I do the hash with this specific structure? THANKS!

Comment on Defining hash structure
Select or Download Code
Re: Defining hash structure
by Anonymous Monk on Aug 25, 2014 at 09:06 UTC

    Does the input actually contain the string "Press <space> to continue, <cr> to single step, ? for help", or is that just from your viewer program?

    It looks like the input consists of fixed-width fields. Have a look at Parse::FixedLength, it may already do most of what you need. The classic approach is unpack with the A template. Otherwise, if the input contains variable-length records separated by a special character (such as tab), Text::CSV is the way to go.

      I changed the content and inserted a ^ delimiter.

      I have this but I don't know how to match the structure:

      use Path::Class; use Data::Dumper; my $dir = dir("C:\\temp\\"); my $file = $dir->file("hashinput.txt"); my $file_handle = $file->openr(); my %slotHash = (); while (my $line = $file_handle->getline()){ if (index($line, "Press", 0) == -1) { $delimiter = quotemeta("^"); my @values = split(/$delimiter/, $line); %slothash = (slot => @values[0] => [type => @values[1]]); } } print Dumper(%slothash);
      Really don't know much about hashes.

        If you write to a hash via %hash = ($key => $value);, it'll overwrite the contents of the hash every time. If instead you want to add data to a hash, you need to write $hash{$key} = $value;. Since your output is a hash containing more hashes, this should be very helpful to you: hashes of hashes. By the way, your last line is better written print Dumper(\%slothash);, you'll see the data structure more clearly.

        Hi mtx,

        Try to write the hash this way:

        $slotHash{$values[0]} = { slotID => $values[0], description => $values[1], hwVersion => $values[2], swVersion => $values[3], uptime => $values[4], };
Re: Defining hash structure
by LanX (Canon) on Aug 25, 2014 at 10:57 UTC
    > Can you guys point me in the right direction?

    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:

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    edit

    introduced temporary @values to "ungolf" code

    update

    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,}/

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-10-31 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (224 votes), past polls