in reply to Parsing a date from comma-delimited file

There seems to be some confusion in responders - you are splitting on a comma, because its a comma separated file. So that makes sense. (Assuming there are no commas in the data itself, of course. If thats even a possibility you should use a CSV module from CPAN.)

The code you have shown should use eq. The == comparison is forcing numeric context, taking as much of the string as it can that looks numeric, in this case everything up to the first /.

Compare:

perl -e 'print "==\n" if "12/Mar" == "12/Jun";' perl -e 'print "eq\n" if "12/Mar" eq "12/Jun";'

(Which give '==' and no output respectively)

You do say you tried eq, so you may have additional problems. If you post more code you'll get a thorough review. (Perhaps more than you want...)

You may also want to look at the many Date modules on CPAN, so you don't require a specific format.

qq

Replies are listed 'Best First'.
Re: Re: Parsing a date from comma-delimited file
by jaco (Pilgrim) on Apr 13, 2004 at 00:34 UTC

    i think qq is correct here when he says you should try using a CSV module from CPAN.DBD::CSV or DBD::AnyData. With it you could just use a select style query on the file from STDIN. like so ...

    use DBD::CSV; my $input = "test.csv"; my $dbh = DBI->connect(qq{DBI:CSV:}); $dbh->{'csv_tables'}->{'info'} = { 'file' => "$input", 'eol' => "\n", 'col_names' => ["ID", "date"]}; my $sth = $dbh->prepare("SELECT * FROM info WHERE date=?"); print "Enter a date to seach for MM/DD/YYYY: "; my $date = <STDIN>; chomp($date); $sth->execute($date); my @array; while(@array = $sth->fetchrow_array){ print "$array[0], $array[1]\n"; }
    with the test.csv being (example)
    1,02/03/2004
    2,09/04/1976
    3,01/13/1998
    4,02/04/2006
    5,02/07/2004
    6,02/03/2004
    
    which should give you want you want

    localhost% ./csv
    Enter a date to seach for MM/DD/YYYY: 02/03/2004
    1, 02/03/2004
    6, 02/03/2004
    

    update:I should pay more attention in examples. I formatted your date incorrectly. It will still work with your format.