CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
parsing a text file column wise

 



teja
New User

Apr 11, 2010, 2:50 AM

Post #1 of 6 (1028 views)
parsing a text file column wise Can't Post

Hi,
I am a beginner perl. I have a text file in following format.


#travel_date^origin^destination^booking_date^party_size
2009-02-12^ZUM^NEK^2008-06-25^1
999-99-99^NPK^GTJ^2009-01-27^2
2008-06-26^AWC^TMA^2008-06-21^1
2009-06-30^OAI^BFN^2008-06-28^1
2008-07-24^KUJ^ASJ^2008-07-04^1


it has five columns delimited by ^. I wanted to fetch every column in to an array then I want to validate every filed.

eg: i have to validate the date having valid values.

Kindly help me how i can do this. Thank you in advance.FrownFrown


7stud
Enthusiast

Apr 11, 2010, 3:20 AM

Post #2 of 6 (1026 views)
Re: [teja] parsing a text file column wise [In reply to] Can't Post


Code
#open the file 

for my $line (<$INFILE>) {
my @fields = split '^', $line;

#validate fields
}

#close the file



(This post was edited by 7stud on Apr 11, 2010, 3:21 AM)


teja
New User

Apr 11, 2010, 5:20 AM

Post #3 of 6 (1020 views)
Re: [7stud] parsing a text file column wise [In reply to] Can't Post

Thank you for the response. I have started with this code.
but i am facing problem. I am not able to split the columns..
actually i have to calculate number of days from travel date to booking date.



#!/user/bin/perl
$file = '/home/ubuntu/Downloads/new.txt';
open(INFO, $file); # open the file
for my $line (<INFO>) # read the file line by line
{
$line_count++;
(@trdate,@orign,@dest,@bkdate,@no) = split '^',$line;

#print $trdate;
print $bkdate;
}
close INFO; # close the file


FishMonger
Veteran / Moderator

Apr 11, 2010, 6:43 AM

Post #4 of 6 (1016 views)
Re: [teja] parsing a text file column wise [In reply to] Can't Post

Add these 2 lines just after the shebang line (that's the first line) and fix the problems that they point out.


Code
use stirct;  

use warnings;



7stud
Enthusiast

Apr 11, 2010, 12:29 PM

Post #5 of 6 (1009 views)
Re: [teja] parsing a text file column wise [In reply to] Can't Post

1) Search google for 'code tags'. Read the first 10 results and come back and post what you have learned.

2) Start all your programs with the following lines:


Code
use strict; 
use warnings;
use 5.010; #if using perl 5.10+


3) Declare all variables with my(), e.g.:


Code
my $num; 
my @numbers;
my($num1, $num2) = (3, 4);
my %phone_number_for;



4) Use the 3-arg form of open(), and use a variable instead of a bareword filehandle:


Code
open my $INFILE, '<', 'data.txt' 
or die "Couldn't open data.txt: $!";



teja
New User

Apr 13, 2010, 12:41 AM

Post #6 of 6 (990 views)
Re: [7stud] parsing a text file column wise [In reply to] Can't Post

Hello..
Thank you for the details. I am zero in perl and i am having problem to understand the terms also. I am asked to implement an algorithm in perl. I am having little idea in unix so I am trying with that knowledge and syntax I am browsing in net.
I have to validate the following data. where you can see the data is not proper. I have to filter the lines where invalid data is given.

2008-12-29^JOB^FFQ^999-99-99^1
2009-04-03^^TCX^2009-01-04^1
999-99-99^NPK^GTJ^2009-01-27^2
^BCJ^DWQ^2008-07-03^0
2009-06-29^MWI^LKM^2009-02-01^2
2009-06-05^FBC^SEL^2008-12-09^1
2009-05-28^TGH^TFA^2008-12-08^1
2009-07-29^77^^2009-02-22^2

The issues I am facing are.
1. Since '^' character has some internal definition in
perl, I am not able to use as delimiter.
2. There are five fields, and I wanted to check if any field is missing.
the scenarios are
- line starting or ending with ^ symbol (^BCJ^DWQ^2008-07-03^0)
- Line having two ^^ symbols (2009-07-29^77^^2009-02-22^2)

for this i am trying to used both grep command.
3. Then I wanted to know, how can I find the difference between the dates given
in first and fourth columns.

I am really sorry for long information, If anyone has an idea to help me, I will be glad. Thank you so much for all those who has taken time to reply.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives