I have a database that I need to massage the format of some dates. After sifting through numerous postings I believe I've finally figured out the bulk of the code I need.

The data looks like this:

26047|9/26/02|js|XBRV|893 Ronson Road||San Francisco|CA|96666|Matt|666 +-650-5114|matt@somewhere.com|710464|1|9/27/02|9/27/02|warranty|Yes| 26048|9/26/02|js|BEVT|13 West Kennedy Blvd.||Yourtown|FL|33456|Christi +an Carusso|713-867-1515||Tel-14|1|||non warranty ``cross ref: 26048|| 26049|9/26/02|js|Ultimate Products|1 South Sunset Drive||Tempe|AZ|8333 +1|Mike Goose|700-676-6006|goose@nowhere.com|RS-502,CC-95|1 each|||S/N + A145902||

What I need to do is change the format of the dates (if they exist) in fields 1, 14, & 15 from the current 9/26/02 format to 26-Sep-2002 format. Thanks to a posting by davorg I think I have a handle on how to do the conversions.

Where I am having a problem is testing if the date fields has data in it (I think):

Use of uninitialized value in numeric gt (>) at /usr/lib/perl5/5.6.1/T +ime/Local.pm line 91, <IN> line 1. Use of uninitialized value in numeric lt (<) at /usr/lib/perl5/5.6.1/T +ime/Local.pm line 91, <IN> line 1. Use of uninitialized value in numeric gt (>) at /usr/lib/perl5/5.6.1/T +ime/Local.pm line 92, <IN> line 1. Use of uninitialized value in numeric lt (<) at /usr/lib/perl5/5.6.1/T +ime/Local.pm line 92, <IN> line 1. Use of uninitialized value in join or string at /usr/lib/perl5/5.6.1/T +ime/Local.pm line 103, <IN> line 1. Use of uninitialized value in join or string at /usr/lib/perl5/5.6.1/T +ime/Local.pm line 103, <IN> line 1. Can't handle date (0, , , 7, 8, -1899) at ./convertdates.pl line 27

Any pointers are appreciated!

#!/usr/bin/perl -w use strict; use Time::Local; my $in_file="default.db"; my $out_file="properdates.db"; my ($sec, $min, $hr)=0; my @fmt_date=''; my @new=''; open (IN,"$in_file")||die "Sorry, but I can't open the file:$!"; open (OUT,">$out_file")||die "Sorry, but I can't append the file:$!"; while(<IN>){ chomp; my @line=split(/\|/); my @date=(split/\|/)[1,14,15]; foreach my $i (@date){ if ($i){ #my ($d, $m, $y)=split/\//, $date[$i]; my ($d, $m, $y)=split/\//, $i; $y-=1900; --$m; my $epoch=timelocal($sec, $min, $hr, $d, $m, $y); ($fmt_date[$i])=strftime('%d-%b-%Y', localtime($epoch)); } #end if } #end foreach push @new, qq($line[0]|$fmt_date[0]|$line[2]|$line[3]|$line[4]|$line +[5]|$line[6]|$line[7]|$line[8]|$line[9]|$line[10]|$line[11]|$line[12] +|$line[13]|$fmt_date[1]|$fmt_date[2]|$line[16]|$line[17]|$line[18]); push @new, "\n"; } #end while print OUT "@new"; close IN; close OUT;

Edited by footpad, ~ Sat Sep 28 02:01:00 2002 (UTC) : Placed <code> tags around error messages, per Consideration.


In reply to Formatting dates by cajun

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.