On Fri, Mar 18, 2011 at 10:43:09PM -0600, Chris Stinemetz wrote:
> Hello,
> 
> I would like to explicitly use printf '<%.2g>' for the element in the array 
> called dist.
> Below is the error I am getting and below that is a sample of the output I 
> can currently generate before I add printf.
> 
> Thank you
> 
> Argument "2,1,1,1175,2.58522727272727,1\n" isn't numeric in printf at 
> ./DOband1.pl line 47.
> <2> 
> 
> 2,1,1,1175,2.58522727272727,1
> 2,1,1,1175,,
> 2,1,1,1175,1.35416666666667,1
> 2,1,1,1175,1.82765151515152,1
> 2,1,1,1175,,
> 2,2,1,1175,,
> 8,1,1,1175,,
> 10,2,1,1175,0.710227272727273,1
> 
> 
> 
> #!/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,
> );

These values don't match the data you have above.  Do you really have
262 items of data per line?

> my @records;
> 
> my @lines = read_file( $filepath );
> 
> chomp @lines;
> 
> foreach my $line ( @lines ) {
> 
>       next unless $line =~ /;/;

Your data is separated by commas, not semicolons.

>       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 ];

Again, commas, not semicolons.

>       $record{carr} = ( $record{chan} == 15 ) ? 2 : 1 ;       
>       $record{dist} = ( length( $record{dist}) > 1 ) ? 
> $record{dist}/6.6/8/2*10/10 : '' ;

This seems strange.

>       push( @records, \%record ) ;
> }
> 
> my @sorted = sort {
>               $a->{cell} <=> $b->{cell} ||
>               $a->{sect} <=> $b->{sect} ||
>               $a->{carr} <=> $b->{carr}
>       } @records ;
> 
> my @report = map 
> "$_->{cell},$_->{sect},$_->{carr},$_->{chan},$_->{dist},$_->{precis}\n" , 
> @sorted;
> #my @report = map "@{$_{ keys %cols }}\n", @records ;
> 
>  printf '<%.2g>', @report ;
> #print @report ;
> write_file($output, @report) ;

And this bit is just wrong.

I don't really know what you are doing here, but this might get you a
little further.  I've left most of it the way you had it and just fixed
up a few problems or made a few changes to get things to "work".


#!/usr/bin/perl

use warnings;
use strict;
use autodie;

my $filepath = 'PCMD';

my %cols = (
        cell    => 0,
        sect    => 1,
        chan    => 2,
        dist    => 3,
        precis  => 4,
);

my @records;
open my $fh, "<$filepath";
while (my $line = <$fh>) {
        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{carr} = ( $record{chan} == 15 ) ? 2 : 1 ;
        $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 ;

for my $s (@sorted) {
        printf "<%.2g>", $s->{$_} for qw(cell sect carr chan dist precis);
        print "\n";
}


-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to