Hello Everybody
I am facing some problems while processing a file containing a several lines like this :
1 1134177124.U.0 1134177124.+.613 1134177163.+.2234 1134208365.D 1134520916.U.0
I need to arrange it like this:
1 1134177124 U 0
1 1134177124 + 613
1 1134177163 + 2234
1 1134208365 D
1 1134520916 U 0
I am using this piece of code but it doesn't process the file as I want:

#!usr/bin/perl
use warnings;
use strict;

my $iq= 'testtrack.txt';
my $id= 'op.txt';
my $b='';
my $a='';
our($length);

open(INFILE,"$iq");
open(OUTFILE,">$id");
 
while(<INFILE>)
 {
   my $line= $_;
   my $temp=$line;
   chomp($line);
   $length = length($line);
   my $i=0;
   my $j=0;
   my $digit_begin;
   $digit_begin=0;   
   while($i <= $length)
  	{
     if($line =~ m/^\d/)
      {
       $a=substr($line,0,1);
       $i++;
       $line=substr($temp,$i,$length);
       print OUTFILE"$a\n";		
       $digit_begin = 1;
      }
     if($line =~ m/^\s/)
      {
       $i++;
       $j=0;
       $line=substr($temp,$i,$length);
       if($digit_begin != 1)
       {
        print OUTFILE"\n$a";
       }
      }
    if($line =~ m/^\d{10}/)
     {
      $b=substr($temp,$i,10);
      print OUTFILE" $b";
      $i=$i+10;
      $line=substr($temp,$i,$length);
      $j=0;
     }
    if($line =~ m/^\./)
     {
      $i++;
      $j=$j+1;
      $line=substr($temp,$i,$length);
     }
    if($line =~ m/[+DU]/)
     {
      $b=substr($temp,$i,1);
      print OUTFILE"\t $b";
      $i++;
      $line=substr($temp,$i,$length);
     }
   }
}

 close(INFILE);
 close(OUTFILE);
 exit;

Can somebody help me with this . Thanks a lot.

cheers
Aj

Recommended Answers

All 2 Replies

use strict;
use warnings;

while (<DATA>)
{
	my $line = $_;
	
	## Split the string use by space
	my @array = split / /, $line;

	## Process the array ( 2 element to end of the array element )
	## Replace default variable dot character into space with globally
	## Concat the array first element to defalut variable
	## Join the each element by use of new line character
	## And it to be stored into $arrange variable
	my $arrange = join "\n", map {s/\./ /g; $array[0]." $_"} @array[1..$#array];
	print "\n$arrange";
}

__DATA__
1 134177124.U.0 1134177124.+.613 1134177163.+.2234 1134208365.D 1134520916.U.0
2 134177124.U.1 1134177124.+.614 1134177163.+.2235 1134208365.E 1134520916.U.1

Thanks a lot for this. I did it myself as well but your code looks much cleaner than what I have done.
I was not aware of the map function earlier. So thanks for enlightening me , I think this function makes things quite simple.

Cheers
Aj

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.