Help with Hash Array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a somewhat complex hash array, below.  I want to know how to
access individual items within the inner hash.

With the example below, say I need the SUBJECT:  $subj =

However, say I need to get the COMMAND from the ALERT array with the
name SENDBUY.  The value I am looking for is: -B

But I am not sure how to turn the ALERT item into a hash so I can
access it by value, rather than by an array index.

Can anyone help??

  'tactical' => {
    'service' => "tactical",
    'url'     => "tacticaltrader",
    'mailer'  => "Tactical Trader Mailer",
    'subject' => "Tactical Trader Summary",
    'alert'   => [
       { 'name' => "GENERATE",     'html' =>
"sid=group_id=29&ctype=0", 'command' => " -t -M",},
       { 'name' => "GENERATEBUY",  'html' =>
"sid=group_id=29&ctype=1", 'command' => " -t -B -M",},
       { 'name' => "GENERATESELL", 'html' =>
"sid=group_id=29&ctype=2", 'command' => " -t -S -M",},
       { 'name' => "SEND",         'html' =>
"sid=group_id=29&ctype=0", 'command' => "",},
       { 'name' => "SENDBUY",      'html' =>
"sid=group_id=29&ctype=1", 'command' => " -B",},
       { 'name' => "SENDSELL",     'html' =>
"sid=group_id=29&ctype=2", 'command' => " -S",},
       { 'name' => "GENERATEINTRA",'html' =>
"sid=group_id=29&ctype=3", 'command' => " -t -I -M",},
       { 'name' => "SENDINTRA",    'html' =>
"sid=group_id=29&ctype=3", 'command' => "",},

Re: Help with Hash Array

Quoted text here. Click to load it

[see original for data structure]

Assuming you just want to search for first matching the alert:

# insert your definition here

sub search_alert
    my ($srvs, $cmd) = @_;

    $cmd eq $_-> and return $_
    for (@});

$buy = search_alert($SERVICES, 'SENDBUY');
print($buy->, "\n") if $buy;

If you need to access alerts regularly both by position (or 'in
order') and by command/ name, you migh want to consider putting them
into the in-order 'alert array' and additionally, putting them into a
hash indexed by name. If you can have more then one pending alert
with a particular name, something more complicated would need to be
done here.

Example 'add an alert' routine for the simpler case (untested)

sub add_alert
    my ($alert, $srvs) = @_;

    $srvs->} = $alert;
    push(@}, $alert);

Re: Help with Hash Array

Quoted text here. Click to load it

Well, it is only unique within each ALERT array.  Each SERVICE,
although they have the same ALERT names, the values will be different
for HTML and COMMAND.  How can I reference the ALERT part by a key for
the NAME?


Re: Help with Hash Array

On 04/30/12 23:52, ExecMan wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

That sounds fine. As I mentioned above...

As long as there isn't another 'name', that's the same, within alert..
e.g. if something like this doesn't happen:

 >>> %SERVICES = (
 >>>     'tactical' =>    {
 >>>       'service' =>    "tactical",
 >>>       'url'     =>    "tacticaltrader",
 >>>       'mailer'  =>    "Tactical Trader Mailer",
 >>>       'subject' =>    "Tactical Trader Summary",
 >>>       'alert'   =>    [
 >>>          { 'name' =>    "GENERATE",     'html' =>
 >>> "sid=group_id=29&ctype=0", 'command' =>    " -t -M",},
              { 'name' =>    "GENERATE",     'html' => "something",
'command' =>    "something",},

Then you can use what I posted. Use the value of what's in 'name'
( e.g. GENERATE ) as a key within the 'alert' hash, instead of an
array.  Depending on what else you're doing, you might not need
the 'alert array', you need an 'alert hash', or you can make
another data structure... maybe 'alert_h' and store the name
as the key, as I showed above.

Try it and if you have questions, show how you're building %SERVICES.
It's probably a matter of changing a push, to do something like my
initial response.

Site Timeline