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

I have a text file that I am parsing in which one of the items is a date formated in the following manner: 12/Mar/2004, this value is in an array of parsed values using
@lineData = split(/\,/, $line);
I then have the user enter a date in the same format and need to see which records have the matching date...

However, I must be doing something wrong. If the user enters 12/Mar/2004 the following line seems to return all dated entries starting with 12.
if ($lineData[18] == $date) { print $lineData[18], "\n"; }

I have also tried using eq and entering the date as 12\/Mar\/2004 but no luck...

20040412 Edit by jeffa: Changed title from 'Simple one... Having Brain Fart...'

Replies are listed 'Best First'.
Re: Parsing a date from comma-delimited file
by jeffa (Bishop) on Apr 12, 2004 at 17:51 UTC

    If you really are comparing a string ... then use the string comparison operator:

    if ($lineData[18] eq $date) {
    You additionally should place a temporary "debug check" to see what is really going on:
    warn "comparing $lineData[18] to $date\n"; if ($lineData[18] eq $date) {
    I'll let someone else comment about about $lineData[18] itself.

    UPDATE: Here is a little test for you:

    no warnings; print '12/Mar/2004' eq '12/Mar/2004' ? 1 : 0; should be 1 print '12/Mar/2004' == '12/Mar/2004' ? 1 : 0; ???? print '31/Mar/2004' eq '12/Mar/2004' ? 1 : 0; should be 0 print '31/Mar/2004' == '12/Mar/2004' ? 1 : 0; ???? __END__ 1 1 0 1
    I'll bet you didn't turn warnings on, did you. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Parsing a date from comma-delimited file
by hardburn (Abbot) on Apr 12, 2004 at 17:50 UTC

    Your date is delimited with the '/' character, but you're splitting on the comma.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Parsing a date from comma-delimited file
by bear0053 (Hermit) on Apr 12, 2004 at 17:49 UTC
    @lineData = split(/\,/, $line); should be... @lineData = split(/\//, $line); #parse 12/Mar/2004 the above results in: $lineData[0] = 12 $lineData[1] = Mar $lineData[2] = 2004 This is causing your comparison: if($lineData[18] == $date) to be wrong
    split divides the string by the character to be split on and returns that many elements to the array. Thus resulting in an array of size 3 for each split per line.
    my $line_date = "$lineData[0]/$lineData[1]/$lineData[2]"; if($line_date eq $date){ print "equal\n"; }
    Use eq for comparing strings instead of ==
      I think there may be some misunderstanding... $lineData is the array which has a bunch of data... item 18 is the element that I am instered in... The entry of the date by the user happens in a command prompt...
Re: Parsing a date from comma-delimited file
by Old_Gray_Bear (Bishop) on Apr 12, 2004 at 17:51 UTC
    You split on '/' into $lineData, so your comparison should be
    if ($date eq "$lineData[18]/$lineData[19]/$lineData[20]") ....

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Parsing a date from comma-delimited file
by qq (Hermit) on Apr 12, 2004 at 18:43 UTC

    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

      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.
Re: Parsing a date from comma-delimited file
by Art_XIV (Hermit) on Apr 12, 2004 at 18:38 UTC

    If the format of the dates in your text file are consistant, there may be no need to parse the dates in it...

    use warnings; use strict; use Date::EzDate; #Date::EzDate can parse several date formats w/o intervention, #so let's make life easy for end-users by making the input format #flexible. my $date_in = 'Mar 13, 04'; my $date = Date::EzDate->new($date_in) or die "Couldn't parse the date '$date_in'.\n"; #convert the date to a string that will match the consistent format #in our DATA file. Note that this will only work if the date format #in the file is, in fact, consistent. my $match_date = $date->{'%f/%h/%Y'}; print "Looking for matches on $match_date:\n"; while (<DATA>) { #a regex match might work as well as splitting #note that there is no need to split the date since we're just #matching strings print if $match_date eq (split)[2]; } 1; __DATA__ blah blah 1/Feb/2004 Gamera blah blah 13/Mar/2004 Godzilla blah blah 22/Jan/2004 Mothra blah blah 3/Apr/2004 Baragon blah blah 13/Feb/2004 Guiron blah blah 5/Feb/2004 King Ghidorah blah blah 27/Mar/2004 Legion blah blah 13/Mar/2004 Mechagodzilla
    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"