Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with Hash Array

Reply
Thread Tools

Help with Hash Array

 
 
ExecMan
Guest
Posts: n/a
 
      04-30-2012
Hi,

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 =
$SERVICES{'tactical'}->{subject};

However, say I need to get the COMMAND from the ALERT array with the
name SENDBUY. The value I am looking for is: trader.pl -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??

%SERVICES = (
'tactical' => {
'service' => "tactical",
'url' => "tacticaltrader",
'from' => "Tactical Trader <tacticaltrader\@mail.com>",
'mailer' => "Tactical Trader Mailer",
'subject' => "Tactical Trader Summary",
'alert' => [
{ 'name' => "GENERATE", 'html' =>
"sid=group_id=29&ctype=0", 'command' => "eod-trader.pl -t -M",},
{ 'name' => "GENERATEBUY", 'html' =>
"sid=group_id=29&ctype=1", 'command' => "trader.pl -t -B -M",},
{ 'name' => "GENERATESELL", 'html' =>
"sid=group_id=29&ctype=2", 'command' => "trader.pl -t -S -M",},
{ 'name' => "SEND", 'html' =>
"sid=group_id=29&ctype=0", 'command' => "eod-trader.pl",},
{ 'name' => "SENDBUY", 'html' =>
"sid=group_id=29&ctype=1", 'command' => "trader.pl -B",},
{ 'name' => "SENDSELL", 'html' =>
"sid=group_id=29&ctype=2", 'command' => "trader.pl -S",},
{ 'name' => "GENERATEINTRA",'html' =>
"sid=group_id=29&ctype=3", 'command' => "trader.pl -t -I -M",},
{ 'name' => "SENDINTRA", 'html' =>
"sid=group_id=29&ctype=3", 'command' => "trader.pl",},
],
},
)
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      04-30-2012
ExecMan <> writes:
>
> 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 =
> $SERVICES{'tactical'}->{subject};
>
> However, say I need to get the COMMAND from the ALERT array with the
> name SENDBUY. The value I am looking for is: trader.pl -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.


[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 $_->{name} and return $_
for (@{$srvs->{alert}});
}

$buy = search_alert($SERVICES{tactical}, 'SENDBUY');
print($buy->{command}, "\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->{alerts_by_name}{$alert->{name}} = $alert;
push(@{$srvs->{alerts_in_order}}, $alert);
}
---------
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      04-30-2012
On 04/30/12 15:40, ExecMan wrote:
> Hi,
>
> 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 =
> $SERVICES{'tactical'}->{subject};


No need for the '->' there..

my $subj = $SERVICES{'tactical'}{ 'subject' };

>
> However, say I need to get the COMMAND from the ALERT array with the
> name SENDBUY. The value I am looking for is: trader.pl -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.


You don't show how you're creating %SERVICES, however...

If the value for 'name' is *always* unique, then you could
use that value as a key, instead of storing things as an array. e.g.

$name = 'GENERATE';
$SERVICES{ 'tactical' }{ 'alert' }{ $name } = {
html => 'sid=group_id=29&ctype=0',
command => 'eod-trader.pl -t -M',
};

Which would let you access the various values directly:

print "command=",
$SERVICES{'tactical' }{ 'alert' }{ 'GENERATE' }{ 'command' },
"\n";


Using your current data structure it's not so bad...

use Data:umper;

for my $rec ( @{ $SERVICES{ 'tactical' }{ 'alert' } } )
{
if( $rec->{ 'name' } eq 'SENDBUY' )
{
print 'command = ', $rec->{ 'command' }, "\n";
}
}

>
> Can anyone help??
>
> %SERVICES = (
> 'tactical' => {
> 'service' => "tactical",
> 'url' => "tacticaltrader",
> 'from' => "Tactical Trader<tacticaltrader\@mail.com>",
> 'mailer' => "Tactical Trader Mailer",
> 'subject' => "Tactical Trader Summary",
> 'alert' => [
> { 'name' => "GENERATE", 'html' =>
> "sid=group_id=29&ctype=0", 'command' => "eod-trader.pl -t -M",},
> { 'name' => "GENERATEBUY", 'html' =>
> "sid=group_id=29&ctype=1", 'command' => "trader.pl -t -B -M",},
> { 'name' => "GENERATESELL", 'html' =>
> "sid=group_id=29&ctype=2", 'command' => "trader.pl -t -S -M",},
> { 'name' => "SEND", 'html' =>
> "sid=group_id=29&ctype=0", 'command' => "eod-trader.pl",},
> { 'name' => "SENDBUY", 'html' =>
> "sid=group_id=29&ctype=1", 'command' => "trader.pl -B",},
> { 'name' => "SENDSELL", 'html' =>
> "sid=group_id=29&ctype=2", 'command' => "trader.pl -S",},
> { 'name' => "GENERATEINTRA",'html' =>
> "sid=group_id=29&ctype=3", 'command' => "trader.pl -t -I -M",},
> { 'name' => "SENDINTRA", 'html' =>
> "sid=group_id=29&ctype=3", 'command' => "trader.pl",},
> ],
> },
> )


 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      04-30-2012
On 04/30/12 17:06, J. Gleixner wrote:
[...]
> Using your current data structure it's not so bad...
>
> use Data:umper;

^^^ Ignore that.. I meant to use that, to show the structure
of $rec, but figured showing the code to get the value might
be better.
>
> for my $rec ( @{ $SERVICES{ 'tactical' }{ 'alert' } } )
> {
> if( $rec->{ 'name' } eq 'SENDBUY' )
> {
> print 'command = ', $rec->{ 'command' }, "\n";
> }
> }


 
Reply With Quote
 
ExecMan
Guest
Posts: n/a
 
      05-01-2012
On Apr 30, 5:06*pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:
> On 04/30/12 15:40, ExecMan wrote:
>
> > Hi,

>
> > 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 =
> > $SERVICES{'tactical'}->{subject};

>
> No need for the '->' there..
>
> my $subj = $SERVICES{'tactical'}{ 'subject' };
>
>
>
> > However, say I need to get the COMMAND from the ALERT array with the
> > name SENDBUY. *The value I am looking for is: *trader.pl -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.

>
> You don't show how you're creating %SERVICES, however...
>
> If the value for 'name' is *always* unique, then you could
> use that value as a key, instead of storing things as an array. *e.g.
>
> $name = 'GENERATE';
> $SERVICES{ 'tactical' }{ 'alert' }{ $name } = {
> * * html * *=> 'sid=group_id=29&ctype=0',
> * * command => *'eod-trader.pl -t -M',
>
> };
>
> Which would let you access the various values directly:
>
> print "command=",
> * * $SERVICES{'tactical' }{ 'alert' }{ 'GENERATE' }{ 'command' },
> * * "\n";
>
> Using your current data structure it's not so bad...
>
> use Data:umper;
>
> for my $rec ( @{ $SERVICES{ 'tactical' }{ 'alert' } } )
> {
> * *if( $rec->{ 'name' } eq 'SENDBUY' )
> * *{
> * * *print 'command = ', $rec->{ 'command' }, "\n";
> * *}
>
> }
>
> > Can anyone help??

>
> > %SERVICES = (
> > * *'tactical' => *{
> > * * *'service' => *"tactical",
> > * * *'url' * * => *"tacticaltrader",
> > * * *'from' * *=> *"Tactical Trader<tacticaltrader\@mail.com>",
> > * * *'mailer' *=> *"Tactical Trader Mailer",
> > * * *'subject' => *"Tactical Trader Summary",
> > * * *'alert' * => *[
> > * * * * { 'name' => *"GENERATE", * * 'html' =>
> > "sid=group_id=29&ctype=0", 'command' => *"eod-trader.pl -t -M",},
> > * * * * { 'name' => *"GENERATEBUY", *'html' =>
> > "sid=group_id=29&ctype=1", 'command' => *"trader.pl -t -B -M",},
> > * * * * { 'name' => *"GENERATESELL", 'html' =>
> > "sid=group_id=29&ctype=2", 'command' => *"trader.pl -t -S -M",},
> > * * * * { 'name' => *"SEND", * * * * 'html' =>
> > "sid=group_id=29&ctype=0", 'command' => *"eod-trader.pl",},
> > * * * * { 'name' => *"SENDBUY", * * *'html' =>
> > "sid=group_id=29&ctype=1", 'command' => *"trader.pl -B",},
> > * * * * { 'name' => *"SENDSELL", * * 'html' =>
> > "sid=group_id=29&ctype=2", 'command' => *"trader.pl -S",},
> > * * * * { 'name' => *"GENERATEINTRA",'html' =>
> > "sid=group_id=29&ctype=3", 'command' => *"trader.pl -t -I -M",},
> > * * * * { 'name' => *"SENDINTRA", * *'html' =>
> > "sid=group_id=29&ctype=3", 'command' => *"trader.pl",},
> > * * *],
> > * *},
> > )

>
>



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?

Thanks!
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      05-01-2012
On 04/30/12 23:52, ExecMan wrote:
> On Apr 30, 5:06 pm, "J. Gleixner"<glex_no-s...@qwest-spam-no.invalid>
> wrote:
>> On 04/30/12 15:40, ExecMan wrote:
>>
>>> Hi,

>>
>>> 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 =
>>> $SERVICES{'tactical'}->{subject};

>>
>> No need for the '->' there..
>>
>> my $subj = $SERVICES{'tactical'}{ 'subject' };
>>
>>
>>
>>> However, say I need to get the COMMAND from the ALERT array with the
>>> name SENDBUY. The value I am looking for is: trader.pl -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.

>>
>> You don't show how you're creating %SERVICES, however...
>>
>> If the value for 'name' is *always* unique, then you could
>> use that value as a key, instead of storing things as an array. e.g.
>>
>> $name = 'GENERATE';
>> $SERVICES{ 'tactical' }{ 'alert' }{ $name } = {
>> html => 'sid=group_id=29&ctype=0',
>> command => 'eod-trader.pl -t -M',
>>
>> };
>>
>> Which would let you access the various values directly:
>>
>> print "command=",
>> $SERVICES{'tactical' }{ 'alert' }{ 'GENERATE' }{ 'command' },
>> "\n";

[...]
>
> 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?



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",
>>> 'from' => "Tactical Trader<tacticaltrader\@mail.com>",
>>> 'mailer' => "Tactical Trader Mailer",
>>> 'subject' => "Tactical Trader Summary",
>>> 'alert' => [
>>> { 'name' => "GENERATE", 'html' =>
>>> "sid=group_id=29&ctype=0", 'command' => "eod-trader.pl -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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Sort by hash vaule, an array of hash references fahdsultan@gmail.com Perl Misc 11 10-10-2005 09:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57