Re: how to implement this with perl



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 once
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);
This approach will not capture the last value on the line if the file
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) {
$data{$labels[$i]}{count}++;
$data{$labels[$i]}{thick} += $thicks[$i];
}
I think this is the same as:
@thicks2 = @thicks;
for my $i ( @labels) {
$data{ $i }{ count }++;
$data{ $i }{ thick } += shift @thicks2;
}
.



Relevant Pages

  • Re: First Perl Program
    ... > # Extract point locations from IGES file and write to new text file. ... > use warnings; ... > use strict; ...
    (comp.lang.perl.misc)
  • Re: Displaying a users group memberships
    ... >> I need to capture a user's group memberships for further processing ... u> use strict; use warnings; ...
    (perl.beginners)
  • Re: splitting paragraph into sentences
    ... > use strict; ... > use warnings; ... The terms you want to capture are seperated by one or more whitespaces - ...
    (comp.lang.perl.misc)
  • Re: how to implement this with perl
    ... regex to extract each column ... use strict; ... use warnings; ... This approach will not capture the last value on the line if the file ...
    (perl.beginners)
  • Re: Multiple Line Extraction
    ... extract the data from the first line and second line onto different ... use strict; ... use warnings; ...
    (comp.lang.perl.misc)