Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Problem extracting data from text file

Question posted by: poolboi (Familiar Sight) on May 12th, 2008 01:48 AM
hi guys

i've having some problem extracting data from a text file
example if i got a text file with infos like:

Date 2008-05-01 Time 22-10
Date 2008-05-01 Time 21-00
Date 2008-05-02 Time 19-15
Date 2008-05-06 Time 18-00


The Date and Time is like one block
but i need to extract say the all the dates
usually i used

Code: ( text )
  1. my $service_line = index ($original, "T11");
  2. my $service = substr ($original, ($service_line), 3);


but the problem is this index points to Date only once
it doesn't go through all the dates...
any idea how i can achieve getting all the dates extracted in the textfile?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
numberwhun's Avatar
numberwhun
Forum Leader
1,727 Posts
May 12th, 2008
02:45 AM
#2

Re: Problem extracting data from text file
Quote:
Originally Posted by poolboi
hi guys

i've having some problem extracting data from a text file
example if i got a text file with infos like:

Date 2008-05-01 Time 22-10
Date 2008-05-01 Time 21-00
Date 2008-05-02 Time 19-15
Date 2008-05-06 Time 18-00


The Date and Time is like one block
but i need to extract say the all the dates
usually i used

Code: ( text )
  1. my $service_line = index ($original, "T11");
  2. my $service = substr ($original, ($service_line), 3);


but the problem is this index points to Date only once
it doesn't go through all the dates...
any idea how i can achieve getting all the dates extracted in the textfile?


Personally, I would open the text file for reading, getting a file handle assigned, then cycle through each line, pulling out the date times. You can either put them out to a file, put them into an array, etc. The choice is yours. You could use something like the following:

Code: ( text )
  1. open(FILE, "<file.txt");
  2. my @array;
  3.  
  4. while(<FILE>)
  5. {
  6.     my $text;
  7.     my $date;
  8.     my $time;
  9.  
  10.     ($text, $date, $time) = split (/\s/, $_);
  11.     push(@array, $date);
  12. }


That should do the trick, but please know its untested.

Regards,

Jeff

Reply
Ganon11's Avatar
Ganon11
Moderator
3,239 Posts
May 12th, 2008
04:10 AM
#3

Re: Problem extracting data from text file
Your index and substr combination is a tried and true method...in C, C++, or Java. Perl has regular expressions, and these are indeed the powerhouse behind Perl (and why I like it so much right now). Here's what I'd do:

Code: ( text )
  1. my @date_list = ();
  2. # Open the file for reading:
  3. open my $filehandle, '<', 'file_name-txt'; # You could add 'or die "Error!  Couldn't open file: $!" after this to error check.
  4. while (<$filehandle>) { # While the file still has contents, read these into $_
  5.    $_ =~ /Date (\d{4}-\d{2}-\d{2})/;
  6.    my $date = $1;
  7.    push @date_list, $date;
  8. }
  9. # Now @date_list holds all the dates, in order of appearance in the text file.

Reply
KevinADC's Avatar
KevinADC
Expert
2,751 Posts
May 12th, 2008
06:27 AM
#4

Re: Problem extracting data from text file
if the lines are fixed length the way to go is unpack(), if not, then regexp, either using split or not.

Reply
poolboi's Avatar
poolboi
Familiar Sight
168 Posts
May 12th, 2008
08:23 AM
#5

Re: Problem extracting data from text file
that's really a lot of suggestion
in the end i did this this

Code: ( text )
  1. my ($data, $data1);
  2. open (FILE1,"file.txt") || die "Couldn't OPEN file!!!\n";   
  3. @$data = <FILE1>;                             
  4. close(FILE1) || die "Couldn't CLOSE file properly!!!\n";   
  5.  
  6. foreach $data1 (@$data)
  7. {
  8. chomp ($data1);
  9. if ($data1 =~ /DATE/){
  10. my $service_line = index ($data1, "DATE");
  11. my $service = substr ($data1, ($service_line+5),10);     
  12. print ("$service\n");   
  13. }}


it did the job too..is my code alright?

Reply
Reply
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Perl Forum Contributors