Nyimi Jose wrote:
>
> IMHO
> $string =~ /.{1,$len}/g;
> Is the best suggestion i have seen so far ;)
>
> I have an other request : "code review" :-)
> Below is my final code.
> I'm sure that you guys will find
> Some better style to write it ...
>
> #!/usr/local/bin/perl -w
> use strict;
> #------------------
> #Global variables
> #------------------
> my $group_len=64;
> my $index_len='A1';
> my $name_len='A6';
> my $way_len='A3';
> my $delim_len='x1';
> my $meas_len='A5';
> my $nof_meas=9;
> #------
> #Main
> #------
> my $file=$ARGV[0];
> open(FH,$file) || die "can not open $file : $! \n";
>
> while(my $line=<FH>){
> next if $. < 3;
>
> my($date,$dummy,$str)=$line=~/(\d{2}-\d{2}-\d{4}:\d{3})\s+(\d{2})(.+)/;
>   for( &split_len($str,$group_len) ){
>      print $date."\t", join( "\t",&get_fields($_) ),"\n";
>   }
> }
> close(FH);
> #------------
> #Subroutines
> #------------
> sub split_len{
>   my ($str, $len) = @_;
>   $str =~ /.{1,$len}/g;
> }
>
> sub get_fields{
>   my($str)[EMAIL PROTECTED];
>   my $format="$index_len $name_len $way_len";
>   $format.=" $delim_len $meas_len" x $nof_meas;
>   unpack($format,$str);
> }

Well you did ask :) How about this.

Cheers,

Rob


use strict;
use warnings;

use constant GROUP_LEN  => 64;
use constant INDEX_LEN  => 1;
use constant NAME_LEN   => 6;
use constant WAY_LEN    => 3;
use constant DELIM_LEN  => 1;
use constant MEAS_LEN   => 5;
use constant NOF_MEAS   => 9;

my $format = join ' ', map "A$_", (
  INDEX_LEN,
  NAME_LEN,
  WAY_LEN,
  (DELIM_LEN, MEAS_LEN) x NOF_MEAS
);

{
  local @ARGV = $ARGV[0];

  while (<>) {

    next if $. < 3;

    my ($date, $str) = / (\d{2}-\d{2}-\d{4}:\d{3}) \s+ \d\d (.+) /x;

    foreach ( split_len ($str, GROUP_LEN) ) {
       print join ("\t", $date, unpack $format, $str), "\n";
    }
  }
}

sub split_len{
  my ($str, $len) = @_;
  $str =~ /.{1,$len}/g;
}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to