0

Hi there,

I'm wondering if there's a way to differentiate a space and a tab in Perl. I have this data format:

02 Aug 10      14:22:00 12345 A Quick Brown Fox Jumps     John Peter     Doe
02 Aug 10      14:25:00 12345 Over The Lazy Dog           John Peter     Doe
13 Aug 10      08:03:25 00001 The Hare and the Turtle     Jane           Doe

I want to have an output like this, for the first row:

item(0) = 02 Aug 10                    --> Date
item(1) = 14:22:00                     --> Time
item(2) = 12345                        --> Employee Number
item(3) = A Quick Brown Fox Jumps      --> Title
item(4) = John Peter                   --> First Name
item(5) = Doe                          --> Last Name

Thanks in advance

Edited by Claude2005: n/a

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Claude2005
1

If the data delimited by a tab (\t)? or is it just spaces? Or is it a fixed length? If it's a tab do this:

use strict;
use warnings;
open (FILE,"<datafile.txt");
my @item;
while(<FILE>){
  chomp;
  (@item)=split(/\t?/);
  #do something with the items because it will be reset next pass
  print "Date: $item[0]\n"; #example
}

If it is fixed length, you can use substr or unpack. Like this:

use strict;
use warnings;
open (FILE,"<datafile.txt");
my @item;
while(<FILE>){
  chomp;
  my $date=substr($_,0,9); #the other locations are left for your work
  print "Date $date\n";
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.