Re: how to implement this with perl
- From: yitzle@xxxxxxxxxxxxxxxxxxxxx (Yitzle)
- Date: Thu, 26 Jun 2008 12:46:47 -0400
On Thu, Jun 26, 2008 at 11:00 AM, Li, Jialin <jialinli1981@xxxxxxxxx> wrote:
another way to handle the one line input is to read the whole line at onceThis approach will not capture the last value on the line if the file
and then use
regex to extract each column
__CODE__
#!/usr/bin/perl
use strict;
use warnings;
my $label_file = "label.in";
my $thickness_file = "thickness.in";
open my $fp_l, "<", $label_file or die "Cannot open $label_file";
my $label = <$fp_l>;
close $fp_l;
open my $fp_t, "<", $thickness_file or die "Cannot open $thickness_file";
my $thick = <$fp_t>;
close $fp_t;
my @labels = ($label =~ /(\d+)\s+/g);
my @thicks = ($thick =~ /(\d*\.\d*)\s+/g);
does not end with whitespace. Split might be better suited.
@labels = split ( $label );
Additionally, (\d*\.\d*) requires that the value has a decimal in it.
A better approach would be to use [\d.]+
for my $i( 0 .. $#labels) {I think this is the same as:
$data{$labels[$i]}{count}++;
$data{$labels[$i]}{thick} += $thicks[$i];
}
@thicks2 = @thicks;
for my $i ( @labels) {
$data{ $i }{ count }++;
$data{ $i }{ thick } += shift @thicks2;
}
.
- Follow-Ups:
- Re: how to implement this with perl
- From: John W. Krahn
- Re: how to implement this with perl
- References:
- how to implement this with perl
- From: Vikingy
- Re: how to implement this with perl
- From: Yitzle
- Re: how to implement this with perl
- From: Jialin Li
- how to implement this with perl
- Prev by Date: Re: How to remove trailing commas and points from a CSV file ?
- Next by Date: Re: parsing a tree like structure
- Previous by thread: Re: how to implement this with perl
- Next by thread: Re: how to implement this with perl
- Index(es):
Relevant Pages
|