spickles has asked for the wisdom of the Perl Monks concerning the following question:
I have an array of hashes that contains information about access points (name, mac address, etc.). I need to add serial numbers to this list, and I have to query the controller based on the AP's MAC address since this is a 1:1 relationship (i.e. there should never be MAC duplicates, and each MAC address has a specific corresponding serial number). Once I parse the response from the controller, I pick out the serial number. Now I need to update the AP hash and make sure the serial number gets paired with the element containing the correct MAC address. How can I do this? My only thought is to search the array each time finding the one that matches (using indices) and then use that current index value to update the hash. As an example:
#!c:/perl/bin/perl use strict; use warnings; my @aps; my $new_hash = {}; #Here I pull in a text file and parse it line by line, which I'm #omitting, but it's a while loop that populates the #hash and pushes to the array $new_hash->{'ap_name'} = "AP One"; $new_hash->{'ap_MAC'} = "00:00:00:00:00:00"; $new_hash->{'ap_model'} = "1131"; push (@aps, $new_hash);
So let's say we use the MAC address of all zeros to query the controller for the serial number. Now I have to find the one and only element in my AoH that has that MAC address and add its serial number. My current thinking is to do this:
for my $i (0 .. $#aps) { if $aps[$i]->{'ap_MAC'} eq "00:00:00:00:00:00" { $aps[$i]->{'serial'} = "FTX12345678"; } }
Rather than do an inefficient search that could potentially search the entire array (if it's the last element that matches) is there a more efficient way of directly accessing this matching element?
Regards,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Add Hash Key Based on Matching Another Key
by Ratazong (Monsignor) on Jul 28, 2010 at 14:50 UTC | |
|
Re: Add Hash Key Based on Matching Another Key
by moritz (Cardinal) on Jul 28, 2010 at 14:50 UTC | |
|
Re: Add Hash Key Based on Matching Another Key
by jethro (Monsignor) on Jul 28, 2010 at 14:53 UTC | |
by spickles (Scribe) on Jul 28, 2010 at 15:51 UTC | |
|
Re: Add Hash Key Based on Matching Another Key
by zek152 (Pilgrim) on Jul 28, 2010 at 16:12 UTC | |
by spickles (Scribe) on Jul 29, 2010 at 15:12 UTC |