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

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";
}

Thanks a bunch!

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.