in reply to Re: csv to flat-file
in thread csv to flat-file

That's just scary. If you're going to solve things that way then just use something like perl -naF, -e 'INIT{ $, = $/ } print @F'. It's going to be more direct and not make tons of eval STRING calls.

Update: There aren't any quoted commas in the original example - this still works fine for normal comma delimited data anyway. (no quoting/escaping allowed)

__SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

Replies are listed 'Best First'.
Re: Re: Re: csv to flat-file
by BrowserUk (Patriarch) on Nov 28, 2002 at 18:00 UTC

    Then main reason is because your version doesn't work. Given this file

    C:\test>type junk.txt 1, 1.2, -3000, fred, "bill & ben", "there's no business like show busi +ness", 'stuff, with, commas'

    Your version does this.

    C:\test>perl -naF, -e"INIT{ $,=$/} print @F" junk.txt 1 1.2 -3000 fred "bill & ben" "there's no business like show business" 'stuff with commas'

    Note

    • the extra space at the beginning of every line except the first.
    • the unstripped quotes
    • the quoted string with embedded commas split into seperate fields.

    Whereas my version does this.

    C:\test>perl -ne "my @a; eval qq[\@a = ($_);];print $_, $/ for @a" jun +k.txt 1 1.2 -3000 fred bill & ben there's no business like show business stuff, with, commas

    I'm not sure what you have against using eval? Your exercising the very same parser everytime you do a list assignment in your program code.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      I suppose. You are still counting on there not being any perl key words being present in the data which was my primary objection.

      __SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

        Ah! The perfect objection for not do it with eval. That small matter had completely gone over my head.

        diotalevi++


        Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
        Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
        Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
        Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.