raisputin has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, I have searched the internet high and low in every way that I can imagine but am unable to find a solution to my problem, so I come seeking the wisdom of those more well versed in the ways of hashes than myself: I run the following command and get the results as shown below:
/usr/local/bin/mpscli -S 10.11.10.2 -N username -P password show subsc +riber accountid="0000000 1111111" NAME: Last, First ACCOUNTID: 0000000 1111111 DESCRIPTION: ORGANIZATION: DEPARTMENT: OFFICE: EMAILADDRESSES: email@host.net BUSINESS1_PHONENUMBERS: BUSINESS2_PHONENUMBERS: HOME1_PHONENUMBERS: 555-1212 HOME2_PHONENUMBERS: CELLNUMBERS: PAGERNUMBERS: ADDRESS1: 150 Main Street ADDRESS2: CITYSTATE: City, State COUNTRYZIP: 99999 NOTE: Test modem @ home LOOKUPKEY: E136470EB3C911DCA53A00E081647FA8 CREATIONTIME: Wed Dec 26 07:47:36 2007 MODIFIEDTIME: Tue Nov 18 11:08:55 2008 CABLEMODEM: MACADDRESS: 000B06227472 MACADDRESS: 0015A2789F9B MTA: DEVICE: HOST:
I am putting all of this data into a hash by splitting the data and making everything before the ":" the Key and then putting in the corresponding data as the value. So far so good :) The problem lies when there are multiple MACADDRESS entries on an account. Typically there is only 1, but there could be as many as n entries there. I would like to put that data into the hash as well so I would get:
MACADDRESS=>6000B0227472 MACADDRESS2=>0015A2789F9B ... MACADDRESSn=><insert mac address here>
I am completely unsure how to do this. Any help is much appreciated. There is probably a MUCH better way to do this than I am doing it, but I understand what I am doing here, and that is important to me, I can evolve it later as my understanding gets better :) Here is the current code that makes the hash.
Notes: 1) @custinfo is where the data is returned from the command shown abo +ve 2) Not all data returned has a : in it, and we don't want that data so + we just work on the data that is seperated by a ':' 3) In the case of the MACADDRESS there are spaces in front of it, so w +e delete them. foreach (@custinfo) { if($_=~/:/){ my ($key, $data) = split (/:/,$_); $key=~s/\s+//g; $record{$key} = $data; } }
Like I said, there is a better way to do this I am sure, but at my current level of understanding, this works :) Not that it matters, but I also delete all hash keys that contain nothing...there is probably a better way to do that so that they are not included in the first place as well...LOL :)

Thanks in Advance

Replies are listed 'Best First'.
Re: Question about a hash....
by ikegami (Patriarch) on Oct 08, 2009 at 18:07 UTC
    for (@custinfo) { next if !/:/; my ($key, $val) = split(/\s*:\s*/, $_, 2); if ($key eq 'MACADDRESS') { push @{ $cust{$key} }, $val; } else { $cust{$key} = $val; } }
Re: Question about a hash....
by SuicideJunkie (Vicar) on Oct 08, 2009 at 18:08 UTC

    How about a HoA structure?

    foreach (@lines) { my ($key, $value) = munge($_); next unless defined $value; # Don't bother creating empty hash entr +ies in the first place ;) push @{$hash{$key}}, $value; }
    That way, you'll end up with an array of mac addresses under the key "MACADDRESS". (And same with any other data that gets repeated)

Re: Question about a hash....
by raisputin (Scribe) on Oct 08, 2009 at 18:19 UTC
    A HA! Thanks guys. Both of you made that seem really easy and I understand it too :)
    *adds this to his arsenal* :) :)