Question for List of hash....

Discussion in 'Perl Misc' started by CatcherInTheRye, Apr 1, 2004.

  1. hi all,

    I am trying to get "cell"and "sector" having the same pn value.
    Temporarily, I make the following script. but don't work..
    How do I do to make it works.

    Thanks in advance,



    #!/usr/bin/perl

    @mscfci = (
    { cell => '3',
    sector => '1',
    pn => '200' },

    { cell => '4',
    sector => '2',
    pn => '300' },

    { cell => '8',
    sector => '2',
    pn => '300' },

    { cell => '9',
    sector => '2',
    pn => '110' },

    { cell => '12',
    sector => '3',
    pn => '300' },

    );

    foreach $fci ( @mscfci ) {

    my @pns = @{ $fci->{pn} };

    if (@pns > 1) {
    foreach $pn (@pns) {
    print $fci->{pn},"\n";
    }
    }
    }
    CatcherInTheRye, Apr 1, 2004
    #1
    1. Advertising

  2. CatcherInTheRye

    Tore Aursand Guest

    On Thu, 01 Apr 2004 14:36:55 +0900, CatcherInTheRye wrote:
    > I am trying to get "cell"and "sector" having the same pn value.


    How come you ended up with thw wrong data?

    > Temporarily, I make the following script. but don't work..


    You should tell us _why_ it doesn't work (ie. any error messages), and/or
    _what_ goes wrong.

    > #!/usr/bin/perl


    Should be:

    #!/usr/bin/perl
    #
    use strict;
    use warnings;

    > @mscfci = (


    Should be:

    my @mscfci = (

    > foreach $fci ( @mscfci ) {
    > my @pns = @{ $fci->{pn} };


    The value which '$fci->{pn}' refers to isn't an array. It's just a
    ordinary scalar.

    > if (@pns > 1) {
    > foreach $pn (@pns) {
    > print $fci->{pn},"\n";
    > }
    > }
    > }


    It looks to me that you're just trying to list all the 'pn' values. That
    could easily be done this way;

    foreach my $fci ( @mscfci ) {
    print $fci->{pn} . "\n";
    }

    The code above will list the 'pn' value for each (hash) entry in the
    '@mscfci' array.

    I think you need to clarify for us what you _really_ want to do. English
    isn't my primary language, so I could use that as an excuse for why I
    didn't understand your problem. :)


    --
    Tore Aursand <>
    "I didn't have time to write a short letter, so I wrote a long one
    instead." -- Mark Twain
    Tore Aursand, Apr 1, 2004
    #2
    1. Advertising

  3. CatcherInTheRye

    ko Guest

    CatcherInTheRye wrote:
    > hi all,
    >
    > I am trying to get "cell"and "sector" having the same pn value.
    > Temporarily, I make the following script. but don't work..
    > How do I do to make it works.
    >
    > Thanks in advance,
    >
    > #!/usr/bin/perl
    >
    > @mscfci = (
    > { cell => '3',
    > sector => '1',
    > pn => '200' },
    >
    > { cell => '4',
    > sector => '2',
    > pn => '300' },
    >
    > { cell => '8',
    > sector => '2',
    > pn => '300' },
    >
    > { cell => '9',
    > sector => '2',
    > pn => '110' },
    >
    > { cell => '12',
    > sector => '3',
    > pn => '300' },
    >
    > );


    As has already been pointed out, if you had were using 'strict' and
    'warnings', you would have received a number of useful error/diagnostic
    messages.

    [snip code]

    One way to do what you want:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my %summary;

    foreach my $pn ( @mscfci ) {
    push @{ $summary{ $pn->{pn} }->{cell} }, $pn->{cell};
    push @{ $summary{ $pn->{pn} }->{sector} }, $pn->{sector};
    }

    while ( my $key = each %summary ) {
    print <<_PN_;
    PN: $key
    CELL: @{ $summary{$key}->{cell} }
    SECTOR: @{ $summary{$key}->{sector} }

    _PN_
    }

    If you need to order the PN values, use a 'foreach' and sort routine
    while iterating over the keys of %summary. And you'll also have to add
    another condition if you don't want duplicate CELL and SECTOR values.

    If you don't understand everything, 'perldoc perldsc' from your shell
    for the relevant documentation.

    HTH - keith
    ko, Apr 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    513
    red floyd
    Nov 10, 2011
  2. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    152
    Ronald Fischer
    Aug 23, 2007
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    610
    David A. Black
    Jul 2, 2008
  4. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    663
    Älphä Blüë
    Jul 18, 2009
  5. Lynn
    Replies:
    5
    Views:
    142
    Anno Siegel
    Sep 12, 2005
Loading...

Share This Page