Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pushing associative arrays

Posted on 1999-07-08
5
Medium Priority
?
237 Views
Last Modified: 2010-03-04
I'm trying to push variables in an associative array, then, with each iteration of the loop, search the array for a particular value.  If I found the value, I want to see what its "association" looks like.

This is the code :

if ($elapsed>0.02) { # I measure something before this
# in the following line I want to check the assoc. array # SLOWHOSTS for a particular ipaddress
foreach $ip (sort keys(%SLOWHOSTS)) {
# if it exists I want to check its association which is
# in a variable called $elapsed
if (/$ipaddress/) { ????; }
# if the ipaddress is not found, I want to add it to the
# associative array with the value of $elapsed so that
# I get something like %SLOWHOSTS=(192.168.0.1,45)
# I tried a lot of variations like push() and the next one
# but nothing seems to work for me
%SLOWHOSTS .= $ipaddress . ',' . $elapsed;
&AlertSpeed;
}
}
0
Comment
Question by:wilfrieds
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:olthoff
ID: 1213960
I assume that you have an IP already.  You seem to be checking the SLOWHOSTS ip's to see if they exist, and they will.  This is the code to see if the IP does not exist and add it.

if (! exists($HASH{$ip})) {
  # The IP does not exist
  $HASH{$ip} = $elapsed;
}; # else it does not...

0
 
LVL 84

Expert Comment

by:ozo
ID: 1213961
if( !exists($SLOWHOSTS{$ipaddres}) ){
     $SLOWHOSTS{$ipaddres} = $elapsed;
     &AlertSpeed;
}
0
 
LVL 1

Author Comment

by:wilfrieds
ID: 1213962
Thanks.  It works, only, I fail to see why it must be so different from normal arrays; or am I missing something?
Anyway, if you want to grab the points, mark it as an answer.
0
 
LVL 1

Accepted Solution

by:
olthoff earned 200 total points
ID: 1213963
Hashes work differently than arrays, arrays are something that can be pushed or shifted because there is some sort of order to it.  Hashes are not in any particular order when you assign a hash 2 values it is hard to say which is actually in the first position.  When you add a third value, it could be the first second or third value.  Because of this shifting and popping aren't possible.

if (! exists($HASH{$ip})) {
  # The IP does not exist
  $HASH{$ip} = $elapsed;
}; # else it does not...
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213964
#it's not so different
$array[3]=$elapsed;
$hash{'three'}=$elapsed;

#you could even say
@array=(@array,$elapsed);
%hash=(%hash,'three',$elapsed);
#although that would be silly.

$HASH{$ip} ||= $elapsed;  #would also work, assuming $elapsed is true
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question