CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Adding element to a hash

 



adiab
New User

Sep 27, 2011, 3:59 AM

Post #1 of 3 (1504 views)
Adding element to a hash Can't Post

Hi,
I'm having problems adding a elements to a hash.

I have the hash definied as follows:

Code
 
$graphs{memory} = {
config => {
args => '--base 1024 --lower-limit 0',
vlabel => 'Bytes Used',
category => 'memcached',
title => 'Memory Usage',
info => 'This graph shows the memory consumption of memcached',
},
datasrc => [
{ name => 'limit_maxbytes', draw => 'AREA', label => 'Maximum Bytes Allocated', min => '0' },
{ name => "bytes.port", draw => 'AREA', label => 'Current Bytes Used', min => '0' },


],
};



and somewhere else in the code I have an if clause which I need to add the new elements:

Code
foreach my $port (@ports){ 

$graphs{memory} -> {datasrc} => { name => "bytes.$port", draw => 'LINE', label => 'Current Bytes Used', min => '0' };


}

The code inside the foreach loop doesn't seem to add anything to my hash. I'm using Data::Dumper to print out the whole thing, but I can't see anything different. Am I missing something obvious?

Thx,
Amro



rovf
Veteran

Sep 27, 2011, 8:00 AM

Post #2 of 3 (1498 views)
Re: [adiab] Adding element to a hash [In reply to] Can't Post

Shouldn't


Code
$graphs{memory} -> {datasrc} => { name => "bytes.$port", draw => 'LINE', label => 'Current Bytes Used', min => '0' }


be


Code
$graphs{memory} -> {datasrc} = { name => "bytes.$port", draw => 'LINE', label => 'Current Bytes Used', min => '0' }


?


FishMonger
Veteran / Moderator

Sep 27, 2011, 8:44 AM

Post #3 of 3 (1496 views)
Re: [adiab] Adding element to a hash [In reply to] Can't Post

Since you're adding a new array element, you need to use push and since I don't like long lines, I'll break it up like this:


Code
push @{ $graphs{memory}{datasrc} }, {  
name => "bytes.$port",
draw => 'LINE',
label => 'Current Bytes Used',
min => 0
};



(This post was edited by FishMonger on Sep 27, 2011, 8:45 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives