I want to be able to dynamically update my hash. I am currently using the following to initialize it:

@Names = split(/\|/, &getIAMparameter(IAMNames)); @Ip = split(/\|/, &getIAMparameter(IAMIp)); %ServerStatus = ( "$Ip[$i]" => ["$Names[$i]", "unknown"], "$Ip[$i+1]" => ["$Names[$i+1]", "unknown"], "$Ip[$i+2]" => ["$Names[$i+1]", "unknown"], "$Ip[$i+3]" => ["$Names[$i+1]", "unknown"], "$Ip[$i+4]" => ["$Names[$i+1]", "unknown"] );
The first two lines of the code goes out and extracts data from a config file, that being server names and IP addresses. With the code shown I have to update it if the number of entries changes in the configuration file. I want to replace it with something like this:
@Names = split(/\|/, &getIAMparameter(IAMNames)); @Ip = split(/\|/, &getIAMparameter(IAMIp)); $num = $#Ip; foreach ($i=0;$i<=$num;$i++) { %ServerStatus = ( "$Ip[$i]" => ["$Names[$i]", "unknown"] ); }
But only get one entry where there should be five. The $num = $#Ip; seems to work just fine, at least it gives me the number back that I am expecting, so I take it that there is something wrong with my foreach loop. I am by no means an expert at hashes (yes, I know, some consider it the life blood of perl!!). I was wondering if someone had a better mouse trap for me.

Many thanks in advance.

Paulster2


You're so sly, but so am I. - Quote from the movie Manhunter.

UPDATE: Thanks for the typo correction, Joost, I have to retype stuff over from another system. That part of the code is typed correctly on that system. I just fat fingered it here. Also, as I stated previously, I have inherited this code from another perl monger who did not use strict. With a couple thousand lines of code, it would make it a little difficult to implement strict now. I try to almost strictly use strict (pun intended). Maybe I will take the time to go back and do that, it may be worth the time, even if for only a learning experience! Thanks again to all for your responses!


In reply to Initializing a hash using a foreach loop by Paulster2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.