Bhagya has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I have a hash bulit has seen below ::
%w = Sub => { + 'rbscells' => [ + 'UtranCell=RNC16-4-1', + 'UtranCell=RNC16-4-2', + 'UtranCell=RNC16-4-3' + ] + },
Please let me know how to retrive all the data in rbscells one by one

Replies are listed 'Best First'.
Re: Usage of hashes with hashes when mulitple values are present
by kcott (Archbishop) on Nov 22, 2013 at 04:54 UTC

    G'day Bhagya,

    "I have a hash bulit has seen below :: %w = Sub => { 'rbscells' => 'UtranCell=RNC16-4-1', 'UtranCell=RNC16-4-2', 'UtranCell=RNC16-4-3' },"

    No you don't!

    As a complete guess, you may want to iterate over the @{$w{Sub}{rbscells}} array.

    Please post your real code; and post it within <code>...</code> tags. See "How do I post a question effectively?" to find out what's wrong with what you've posted. See "How do I change/delete my post?" for information about updating it.

    -- Ken

Re: Usage of hashes with hashes when mulitple values are present
by lune (Pilgrim) on Nov 22, 2013 at 11:20 UTC
    Your code gives me the error
    Odd number of elements in hash assignment at ./t4.pl line 6.
    Hashes are defined this way:
    my %hash = ( somekey => somevalue, ...);
    So your code should read:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my %w = ( Sub => { 'rbscells' => 'UtranCell=RNC16-4-1', 'UtranCell=RNC16-4-2', 'UtranCell=RNC16-4-3' } ) ; print Dumper(%w);
    Now when you look at the output of Data::Dumper, you can see, that what you probably wanted to be an array (the three strings starting with "UtranCell*") are in fact a hash value and another hash key + value:
    $VAR1 = 'Sub'; $VAR2 = { 'rbscells' => 'UtranCell=RNC16-4-1', 'UtranCell=RNC16-4-2' => 'UtranCell=RNC16-4-3' };
    However, you can't just put an array definition inside a hash - from perldsc:
    The most important thing to understand about all data structures in Perl--including multidimensional arrays--is that even though they might appear otherwise, Perl @ARRAYs and %HASHes are all internally one-dimensional. They can hold only scalar values (meaning a string, number, or a reference). They cannot directly contain other arrays or hashes, but instead contain references to other arrays or hashes.
    So another change, to define an array reference:
    ... 'rbscells' => [ 'UtranCell=RNC16-4-1', 'UtranCell=RNC16-4-2', 'UtranCell=RNC16-4-3' , ] ...
    You can access the elements of the array as always, via index starting from 0 or with foreach like this:
    foreach my $element (@{$w{Sub}->{rbscells}}) { print $element . "\n"; }
    To understand nested data structures in perl, please refer to perldsc, especially the chapter about "Hashes of Arrays".
      Hi , Please find this piece of code as follows ::
      # Get the list of cells, which has same frequency and SC Id # $t = $w{$_}{frequency} .':'. $w{$_}{sc}; push(@{$w{$t}{cells}}, $_);
      . Here w is a hash variable . for each cell we will be retrieving its corresponding frequency and sc value in following format repectively :
      $w{$_}{frequency} and $w{$_}{sc}
      The above piece of code will collect all the cells which fall in that frequency and sc value .. SO when we give data dumper we will get as follows ::
      '-1:130' => { 'cells' => [ 'SubNetwork=ONRM_ROOT_MO_R,SubNet +work=RNC12,MeContext=RNC12,ManagedElement=1,RncFunction=1,UtranCell=R +NC12-8-2' 'SubNetwork=ONRM_ROOT_MO_R,SubNetwork=RNC12,MeContext=RNC12,ManagedEle +ment=1,RncFunction=1,UtranCell=RNC12-8-8' ] },
      Please let me know how to retrieve the values taht are in array .
        It is hard to follow you, as you omit certain information, that would make it much easiear to answer your question.

        You should just post a runnable piece of complete code, that shwow your problem. Please read ?node_id=510718

        As you just want to know how to *access* the data, why bother about the way you *create* the data structure? Here, I just initialized a hash with the data you provided. Then I can access the elements of the arrayref:

        #!/usr/bin/perl -w use strict; use Data::Dumper; my %w = ( '-1:130' => { 'cells' => [ 'SubNetwork=ONRM_ROOT_MO_R,SubNetwork=RNC12,MeContext=RNC1 +2,ManagedElement=1,RncFunction=1,UtranCell=RNC12-8-2', 'SubNetwork=ONRM_ROOT_MO_R,SubNetwork=RNC12,MeContext=RNC1 +2,ManagedElement=1,RncFunction=1,UtranCell=RNC12-8-8' ], }, ); #print Dumper(\%w); # access one element by index: first = 0 print "Element 0: " . $w{'-1:130'}{'cells'}->[0] . "\n"; print "\n"; # access all available in a foreach loop foreach my $element (@{$w{'-1:130'}{'cells'}}) { print $element . "\n"; }

Re: Usage of hashes with hashes when mulitple values are present
by marinersk (Priest) on Nov 22, 2013 at 06:52 UTC
    If it's a comma separated list, use split.
    my @members = split /\,/, $hash{$key};

    If it's a hash of arrays, use foreach.
    foreach my $member (@{$hash{$key}})

    If it's a hash of hashes, just pull out the data.
    foreach my $member (keys $hash{$key})

    For more details, show some code and explain what isn't working as expected.