Change your data structure to be closer to how your data actually looks.
I.E. instead of squeezing a hashref into an array, make it a real hashref:
Replace "[]" with "{}" in your data;
You can always "SORT" it later, if necessary.
my %INPUT_DATA = (
'DEVICE1_NAME' => {
'1/1/5/40' => {
'ACTUAL' => {
'SVC' => '239',
'SPTM' => '112',
},
'NEW' => {
'SVC' => '239',
'SPTM' => '183',
},
},
}, ...
# Now, you can use:
#....set $device_name
#....
foreach my $current_port ( sort keys %{$INPUT_DATA{$device_name}} ) {
my @CURRENT_PORT_STATUS = print( "show xdsl operational\-data line
+ $current_port detail");
}
A simple sort is used above. In real life, your sort sub may be complicated if you want to preserve true numeric order in the port.
One alternative is to add an ORDER field within the port info, and manually increment it. That field can subsequently be used as a sort key. There are also modules that provide (tied) ordered-key hashes. (eg:
Data::XHash).
Update: If you really wanted to keep the current data structure (in case it came from outside your control), you will need to process every other element : i.e. skip the hashref, process only the "key" which is a scalar.
To do this, the first thing in the "for loop" should be:
next unless ref $current_port eq ""; # skip if $current port is NO
+T a scalar.
"I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
-- Dr. Cox, Scrubs
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.