Sure lots of ways, this is Perl ;-)...

Jose Malacara wrote:
Hello.

I have a script that parses csv files for the occurence of an IP address that is passed to the script as an argument when it runs. Currently, I can only run the script from within the same directory as my data files. I would like to be able to move my csv files into a separate data directory, but I seem to be having problems mapping the rest of the script to the new directory. Is there any way to make my input look something like this:

my $input="data/$file" or is there a better way to handle this?

Thanks,
Jose


Any help would be appreciated. Here's what' I've got so far:


#!/usr/bin/perl -w
use strict;
use Text::CSV;

my $ip=$ARGV[0]; my $csv=Text::CSV->new();
opendir(DIR, ".");
opendir(DIR, 'data') or die "Can't open directory for reading: $!";

Is probably the easiest, cheapest, and ugliest, but thats better than leaving alone isn't it ;-) ??

You could consider passing in the directory that contains your files to be parsed, so that your opendir is not hardcoded:

my $dir = $ARGV[1];
opendir(DIR,$dir) or die "Can't open directory for reading: $!";

> command.pl 127.0.0.1 /tmp/data

You could also consider passing in the files you want to parse, then doing something like,

my $ip = shift @ARGV;
foreach my $file (@ARGV) {
# parse your file
}

> command.pl 127.0.0.1 /tmp/data/*.csv

You could go down the path of File::Find, or you could use Getopt::Long or Getopt::Std and have the user specify whether they are passing in a directory, a list of files, or nothing (in which case you just mangle the current directory).....

http://danconia.org



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to