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

AT the moment, my database is spaced delimited, with each field having a set amount of space. I want to convert to comma delimited and was wondering what the is needed to search and output the findings.
  • Comment on How do I search and output results to HTML from a CSV database file?

Replies are listed 'Best First'.
Re: How do I search and output results to HTML from a CSV database file?
by asiufy (Monk) on May 15, 2001 at 08:47 UTC
    Well, it's late here, but this will get you started:
    #!/usr/bin/perl -w use strict; open (FILE1, "input.txt"); open (FILE2, ">output.txt"); while (<FILE1>) { /^(.+?)\s{1,9}(.+?)\s{1,9}(.+?)$/; print FILE2 "$1,$2,$3\n"; } close FILE2; close FILE1;
    Open two files, one for input and the other for output. Read each line in the first, matching the fields. On my example, I match (barely, that regexp is totally half-baked) 3 fields, with 1 up to 9 spaces between them. Store the matched fields in the regexp's history variables ($1/$2/$3), then output that to the file.

    Is this what you wanted?
Re: How do I search and output results to HTML from a CSV database file?
by ColtsFoot (Chaplain) on May 15, 2001 at 09:34 UTC
    I would use code similar to asiufy but within
    the while loop I would use the split
    function
    while ($line = <FILE1>) { @fields = split / /, $line; $output = pop @fields; foreach $field (@fields) { $output = $output . ',' . $field; } print <FILE2> qq($output\n); }
    This should cope with any number of fields and
    any number of spaces between fields.

    Hope this helps
      Indeed. But we can tidy up a bit:
      while( <FILE> ) { split; print FILE2 join ',' , @_; print FILE2 "\n"; }
      Here you see that the perl shorthands improve readibility.

      And this even compacts to (trading readability a bit):

      print FILE2 join( ',', split )."\n" while <FILE>; #or use SuperSplit; print FILE2 superjoin( ',', supersplit_open( $filename ) );

      Jeroen
      "We are not alone"(FZ)

        Jeroen your solutions realy show the power of perl
        As Sasquire was an "Initiate" I felt that my solution
        was a little easier to follow
Re: How do I search and output results to HTML from a CSV database file?
by bjelli (Pilgrim) on May 15, 2001 at 15:48 UTC

    nice soultions everyone, but sadly not for this problem. You are all parsing space-seperated data, but Sasquires data ist in fixed-width fields.

    If you've got just a few fixed-width fields, you might want to try substr. If you have many fields, I would recommend unpack, like so:

    while(<DATA>) @data = unpack("A8A10A2",$_); print join(",", @data), "\n"; } __DATA__ 12345678123456789012 longone evenlongerha a b c

    the template string "A8A10A2" tells unpack to look for 8 Ascii Characters, then 10 Ascii Characters, then 2 Ascii Characters. unpack is also used a lot in handling binary data (which I don't understand). I just stick to this usage.

    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at
      which in turn can be shortened to
      print join(",",unpack("A8A10A2",$_)),"\n" while <DATA>;
      and run from the command line as
      perl -i.bak -l -ne "print join(',',unpack('A8A10A2',$_));" filename
      this will do the conversion for you. I'm sure there's a module out there which parses comma delimited files into html tables. hope this helps

      larryk

      ---------------------------------------------------- $less->{'chars'} = `"time in the pub" | more`; # :-D
        The module you're thinking of is Data::Table. From the docs:
        $t = Data::Table::fromCSV("aaa.csv"); # Read a csv file into a table +oject print $t->html; # Display a 'portrait' HTML TAB +LE on web.
        buckaduck