Question for List of hash....

  • Thread starter CatcherInTheRye
  • Start date
C

CatcherInTheRye

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";
}
}
}
 
T

Tore Aursand

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. :)
 
K

ko

CatcherInTheRye said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top