Data Conversion

Discussion in 'Data Sets and Feeds' started by Stardust, Apr 29, 2006.

  1. This is my first attempt at perl and regex's:

    Code:
    $data_file="some_input_file.txt";
    $data_file_out="out_$data_file";
    
    open(DAT, $data_file) || die("Open input file Failed..");
    open(DATOUT, ">$data_file_out") || die("Open output file failed..");
    
    @raw_data=< DAT>;
    foreach $line (@raw_data)
    {
      @array=split(/,/, $line);
      $m='/';
      foreach (@array) {
        if (/([0-9]+)[$m]([0-9]+)[$m]([0-9]+)/) {
        print DATOUT "$3$1$2,";
        }
        else {
          if (!/\n/){
            print DATOUT "$_,";
          }
          else {
            print DATOUT "$_";
          }
        }
      }
    }
    
    It doesn't pass the file name as a parameter which obviously would be better. (just learning on the fly). Not sure how I would use Rosy's method when trying to extract the date from the string.. Once the regex has found the date within the string there does not seem to be much need for the fancy Date::Manip stuff. Maybe someone can improve this simple script, it's a decent perl intro tutorial.. I am discovering that perl is pretty interesting actually. Fortunately, Qt provides a fully featured Regular Expressions class.

    I am sure someone can compact this down to a few lines.. :)
     
    #11     Apr 30, 2006