Hash

P

Pradeep Patra

Hi all,
I am new to perl. While doing some sample programs I am getting a
issue as follows.

I have a HASH as follows
$VAR1 = [
{
'A-list' => [
{
'B-info' => [


{


'name' => 'd',


'time' => '0',


'virtual' => 'v1',


'f1' => '0'
}.


{


'name' => 'n2',


'time' => '0',


'virtual' => 'v1',


'f1' => '0'
}.


]


I want to search the line virtual="v1" from all the records in the
above hash and then in that record i want to check name is "d" and if
it is true return 1 else return 0. I am trying to do as follows for
testing whether the name returns "d" or not. But $name returns empty
string. Can anybody tell me what I am doing wrong for which i will
appreciate?


my $name = $VAR1{"A-list"}->{"B-info"}->[0]->{'name'};


my $name = $VAR1{"A-list"}->{"B-info"}->[0]->{"name"}; (Same results
$name returns empty string)
 
G

George Mpouras

Στις 15/3/2011 7:58 μμ, ο/η Pradeep Patra έγÏαψε:
Hi all,
I am new to perl. While doing some sample programs I am getting a
issue as follows.

I have a HASH as follows
$VAR1 = [
{
'A-list' => [
{
'B-info' => [


{


'name' => 'd',


'time' => '0',


'virtual' => 'v1',


'f1' => '0'
}.


{


'name' => 'n2',


'time' => '0',


'virtual' => 'v1',


'f1' => '0'
}.


]


I want to search the line virtual="v1" from all the records in the
above hash and then in that record i want to check name is "d" and if
it is true return 1 else return 0. I am trying to do as follows for
testing whether the name returns "d" or not. But $name returns empty
string. Can anybody tell me what I am doing wrong for which i will
appreciate?


my $name = $VAR1{"A-list"}->{"B-info"}->[0]->{'name'};


my $name = $VAR1{"A-list"}->{"B-info"}->[0]->{"name"}; (Same results
$name returns empty string)







$VAR1 =
[
{
'A-list'=>
[
{
'B-info'=>
[
{'name'=>'d' ,'time'=>'0','virtual'=>'v1','f1'=> '0'},
{'name'=>'n2','time'=>'0','virtual'=>'v1','f1'=> '0'}
]
}
]
}
];



my $alias = $VAR1->[0]{'A-list'}[0]{'B-info'}[0];

print (($alias->{'virtual'} eq 'v1' && $alias->{'name'} eq 'd')?1:0);



#Peace
 
J

Jim Gibson

Pradeep Patra said:
Hi all,
I am new to perl. While doing some sample programs I am getting a
issue as follows.

I have a HASH as follows
$VAR1 = [

The '[' indicates an array reference, not a hash and not a hash
reference. The elements of the array can be referenced with the
notation $VAR->[0], $VAR->[1], etc. Those elements happen to be
references to a hash.

{
'A-list' => [

The value of the element of the hash referred to by $VAR->[0] that has
the key 'A-list' is a reference to an array. The elements of this array
are $VAR->[0]->{'A-list'}->[0], etc.
{
'B-info' => [


{


'name' => 'd',


'time' => '0',


'virtual' => 'v1',


'f1' => '0'
}.


{


'name' => 'n2',


'time' => '0',


'virtual' => 'v1',


'f1' => '0'
}.


]


I want to search the line virtual="v1" from all the records in the
above hash and then in that record i want to check name is "d" and if
it is true return 1 else return 0. I am trying to do as follows for
testing whether the name returns "d" or not. But $name returns empty
string. Can anybody tell me what I am doing wrong for which i will
appreciate?


my $name = $VAR1{"A-list"}->{"B-info"}->[0]->{'name'};

my $name = $VAR->[0]->{'A-list'}->[0]->{'name'};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top