Sherlock Perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I have several questions and I hope there are people with free time and desire to help me :) Please be patient because I wrote a line in Perl for the first time around 25/26 hours ago. I need to get to a .csv file in an ftp and then parse this file to find a specific numerical value and then I need to export it to sql but lets do it step by step :D So here is my code connecting to the ftp and trying to get the csv file. Can you please tell me if it is correct and if so how should I proceed? Open/Activate the file and try to "look" inside? I already have an idea how to find the value but NO IDEA how to do it in Perl :/ I hope you will bear with me. So here is the code (and thank you in advance):

#!/usr/bin/perl use strict; use IO::File; use File::Copy; use Net::FTP; sub main(){ my $ftp = Net::FTP->new("ftp://.....com"); $ftp->login('.....','.....'); + $ftp->binary(); $ftp->cwd('/a/b/c'); + my @ftpFiles =$ftp->ls(); + + my $i = 0; + + + while ($i < $#ftpFiles) { + + if($ftpFiles[$i] =~ m/andv/) + { $ftp->get($ftpFiles[$i +]); $myfile = $ftp; } $i++; } $ftp->quit();

Replies are listed 'Best First'.
Re: Parse .csv file from FTP using Perl
by muba (Priest) on Jun 21, 2012 at 00:27 UTC

    You're starting off great. I see you use strict;. Might I suggest that you use warnings; as well? And I see you've discovered how to work with modules, so you're on a good track here.

    As for reading the CSV file, how about Text::CSV? There is a nice section about database programming in our tutorials library, you might want to have a look at that.

    Good luck! Should you run into further problems, don't hesitate to ask.

      Thank you for the quick reply. Could you please direct me to some place where I can read how to parse the csv. The idea is that the file I am reaching to has 2 rows and I want to find the value in the 2nd row that corresponds to a specific value (assume "xyz") in the first row? How exactly should I go about doing this and storing the value from the 2nd row? Thank you in advance!

        Text::CSV will help you do that. I would hack together a little something for you, but uhm, you also need to know which field (or column, or whatever the CSV terminology for that is) the value appears in, am I right? Otherwise you're just saying something like, "search me a thing that is similar to the thing that grows on the tree!" Yeah, okay, but what sort of thing on what tree? Are we talking branches here? Apples? Pears? Leafs?