User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 456,597 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,453 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser: Programming Forums
Views: 2448 | Replies: 5 | Solved
Reply
Join Date: Sep 2007
Location: Near Arctic Circle
Posts: 5
Reputation: DaveHammer is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
DaveHammer DaveHammer is offline Offline
Newbie Poster

Question Need wisdom on how to read Comma Delimited unnamed Files with PERL?

  #1  
Sep 11th, 2007
Hi all,

I am in a situation where I don't know much PERL/CGI, but am frantically reading through the learn Perl/learnCGI in 24 hour books. I just got done programing in dbase for some climatology work and now have to switch gears quickly.

My mission: to read in a (comma-delimited)web file with no extension:
http://147.241.74.132/cgi-bin/model/...fdda/&BIAS=OFF
The problem is, that I want to skip down 14-16 lines to get to the first line of data.

What is the easiest way to read in the lines of data, into an array?

A friend sent me this link: CGISCRIPTER

Is this along the lines of what I need? i will be placing simple (~50)Excel line equations probably into an array as well to perform calculations with the read in values. I will output the answers to a simple HTMl page.

HELP! I don't know much in the realm of CGI scripting, but I have been learning MySQL. I read in one message board that you can use "LOAD DATA INFILE 'C:\Documents and Settings\d~1\Desktop\test.csv' INTO TABLE x; " or something along those lines? Could MySQL be used or is just faster just to have PERL do all the reading, writing and arithmetic?

Anyways, thanks to anyone who would lend a helping thought,
Dave

PS- In the perl books, where should I concentrate my reading? using the "OPEN" command?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2006
Posts: 644
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: Need wisdom on how to read Comma Delimited unnamed Files with PERL?

  #2  
Sep 11th, 2007
You can certainly use the open() command to read a file, any file, as long as your script has permission to open the file. We will assume it does.

There are perl modules for reading CSV files too:

http://search.cpan.org/~hmbrand/Text...0.31/CSV_XS.pm

This gets into the "you need to install the module" discussion though. But if you can tackle the installation problem the above module works well.

You may very well be able to use MySQL but you need to ask on the MySQL forum.
Reply With Quote  
Join Date: Sep 2007
Location: Near Arctic Circle
Posts: 5
Reputation: DaveHammer is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
DaveHammer DaveHammer is offline Offline
Newbie Poster

Re: Need wisdom on how to read Comma Delimited unnamed Files with PERL?

  #3  
Sep 11th, 2007
Hi Kevin,

thanks for your thoughts. I have successfully read in the file (that I saved to my pc), but when I try to place the URL inside the quotes I get errors.

To retrieve the file, do I have to use some sort of "use HTML::LinkExtor;" ? LWP?
Reply With Quote  
Join Date: Mar 2006
Posts: 644
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: Need wisdom on how to read Comma Delimited unnamed Files with PERL?

  #4  
Sep 12th, 2007
I don't understand what you are asking. Post your perl code.

Are you trying to use the open() function to read a file on another server? That will not work.
Reply With Quote  
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 176
Reputation: trudge is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Need wisdom on how to read Comma Delimited unnamed Files with PERL?

  #5  
Sep 12th, 2007
Originally Posted by DaveHammer View Post
Hi all,

I am in a situation where I don't know much PERL/CGI, but am frantically reading through the learn Perl/learnCGI in 24 hour books. I just got done programing in dbase for some climatology work and now have to switch gears quickly.

My mission: to read in a (comma-delimited)web file with no extension:
http://147.241.74.132/cgi-bin/model/...fdda/&BIAS=OFF
The problem is, that I want to skip down 14-16 lines to get to the first line of data.

What is the easiest way to read in the lines of data, into an array?

A friend sent me this link: CGISCRIPTER

Is this along the lines of what I need? i will be placing simple (~50)Excel line equations probably into an array as well to perform calculations with the read in values. I will output the answers to a simple HTMl page.

HELP! I don't know much in the realm of CGI scripting, but I have been learning MySQL. I read in one message board that you can use "LOAD DATA INFILE 'C:\Documents and Settings\d~1\Desktop\test.csv' INTO TABLE x; " or something along those lines? Could MySQL be used or is just faster just to have PERL do all the reading, writing and arithmetic?

Anyways, thanks to anyone who would lend a helping thought,
Dave

PS- In the perl books, where should I concentrate my reading? using the "OPEN" command?


First I must warn you - you can't 'learn' any programming language in 24 hours. And forget about using MySQL for solving your problem. It's enough you have one learning curve to learn.

To address your immediate question 'to skip down 14-16 lines', Perl has a special variable called $. - that is a dollar sign with a dot immediately following it.

It is the current line counter, and is used something like this:

open IN ,'<', $somefile or die "Can't open $somefile: $!\n"; # ALWAYS check open result
while (defined ($line = <IN>))
{
print "Current line = $.\n";
}
close IN or die "Can't close $somefile: $!\n"; # and check the close result

So, in your case you want to read the file until $. equals 14 or 16. I leave that as an exercise for you.

You will have to provide more details to determine whether to read 14, 15, or 16 lines.
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote  
Join Date: Sep 2007
Location: Near Arctic Circle
Posts: 5
Reputation: DaveHammer is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
DaveHammer DaveHammer is offline Offline
Newbie Poster

Re: Need wisdom on how to read Comma Delimited unnamed Files with PERL?

  #6  
Sep 15th, 2007
Thanks for your help everyone. It pointed me to some new options in the coding. I didn't see the last post by trudge until now. I ended up ingesting the webfile into an array and then using:
@temparray= split/([,' '])/, $hours[$i];

which tells it whenever there is a comma and/or a space then split it. It took me forever to find this code, but it works great!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 6:58 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC