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 )
my $service_line = index ($original, "T11"); 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).
|
|
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 )
my $service_line = index ($original, "T11"); 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 )
open(FILE, "<file.txt"); my @array; while(<FILE>) { my $text; my $date; my $time; ($text, $date, $time) = split (/\s/, $_); push(@array, $date); }
That should do the trick, but please know its untested.
Regards,
Jeff
|
|
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 )
my @date_list = (); # Open the file for reading: open my $filehandle, '<', 'file_name-txt'; # You could add 'or die "Error! Couldn't open file: $!" after this to error check. while (<$filehandle>) { # While the file still has contents, read these into $_ $_ =~ /Date (\d{4}-\d{2}-\d{2})/; my $date = $1; push @date_list, $date; } # Now @date_list holds all the dates, in order of appearance in the text file.
|
|
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.
|
|
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 )
my ($data, $data1); open (FILE1,"file.txt") || die "Couldn't OPEN file!!!\n"; @$data = <FILE1>; close(FILE1) || die "Couldn't CLOSE file properly!!!\n"; foreach $data1 (@$data) { chomp ($data1); if ($data1 =~ /DATE/){ my $service_line = index ($data1, "DATE"); my $service = substr ($data1, ($service_line+5),10); print ("$service\n"); }}
it did the job too..is my code alright?
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
|