I am trying to code a foreach loop, that will add all occurrences of the
element rlptxat that have the same elements cell, sect and chan.
My failed attempt can be located in between the ### lines.
Below is what I have so far.
Thank you in advance,
Chris
#!/usr/bin/perl
use warnings;
use strict;
use File::Slurp;
my $filepath = 'C:/temp/PCMD';
my $output = 'output.txt';
my %cols = (
cell => 31,
sect => 32,
chan => 38,
dist => 261,
precis => 262,
rlptxat => 44,
);
my @records;
my @lines = read_file( $filepath );
chomp @lines;
foreach my $line ( @lines ) {
next unless $line =~ /;/;
my %record;
# this gets just what you want into a hash using a hash slice and an # array
slice.
# the order of keys and values will be the same for any # given hash
@record{ keys %cols } = (split /;/, $line)[ values %cols ];
####################################################################
$record{rlptxat} = ( length($record{rlptxat})> 1 ) ?
$record{rlptxat}{cell, sect, chan, dist}++ : '' ;
####################################################################
$record{carr} =
( $record{chan} == 75 ) ? 2 :
( $record{chan} == 1025 ) ? 2 : 1 ;
#1; #
Common carrier
$record{dist} = ( length( $record{dist}) > 1 ) ?
$record{dist}/6.6/8/2*10/10 : '' ;
push( @records, \%record ) ;
}
my @sorted = sort {
$a->{cell} <=> $b->{cell} ||
$a->{sect} <=> $b->{sect} ||
$a->{carr} <=> $b->{carr}
} @records ;
my @report = map "$_->{cell}\t$_->{sect}\t$_->{carr}\t$_->{chan}\t$_->{dist}\n"
, @sorted;
print @report ;
write_file($output, @report) ;
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/