in reply to Re: wanting to make a file of strings usable by SQL
in thread wanting to make a file of strings usable by SQL

Within the while loop I believe I need to test if I am at the last line of the file; if I am, I exit the loop and process the last line outside the loop. sed avoids that dificulty because it lets me overwrite my last line, removing the trailing comma and adding the closing paren.

I am thinking that reading the whole file into an array might turn out to be the simplest way to do this after all.

  • Comment on Re^2: wanting to make a file of strings usable by SQL

Replies are listed 'Best First'.
Re^3: wanting to make a file of strings usable by SQL
by BrowserUk (Patriarch) on Dec 27, 2005 at 03:45 UTC

    Try this

    #! perl -slw use strict; chomp( $_ = <DATA> ); print "($_,"; while( <DATA> ) { chomp; $_ = "'$_'"; last if eof(DATA); print "$_,"; } print "$_)"; __DATA__ one two three four

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Ugh. That is definitely much less clear than his sed script. Without knowing what it’s supposed to do it’ll take blinking and staring to figure it out.

      Rule of thumb: eof is about as meritorious as goto.

      Makeshifts last the longest.

        Rule of thumb: eof is about as meritorious as goto.

        In a word. Balderdash :)

        $. Is that the commercial arm of /.?

        Quite how you can claim that a comparison using eof is less clear than one using $. is quite beyond me.

        Most anyone who has programmed in any C-like language, and a whole host of others, will immediately recognise the concept and purpose of the function eof.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: wanting to make a file of strings usable by SQL
by Aristotle (Chancellor) on Dec 27, 2005 at 04:51 UTC

    No, you just leave the line unterminated at the end of the loop and then check at the next iteration whether there was a previous line that needs terminating. The only time when that isn’t true is on the first line, which is a very simple condition. This leads to my trivial code below.

    Makeshifts last the longest.

Re^3: wanting to make a file of strings usable by SQL
by blazar (Canon) on Dec 27, 2005 at 15:51 UTC
    I am thinking that reading the whole file into an array might turn out to be the simplest way to do this after all.

    Well, if your input files are not huge and you don't make this into a (bad) habit, that should be fine:

    my @lines=map {chomp; "'$_'"} <>; $"=",\n"; print "(@lines)\n";

    Mind you: it can be done in less obscure ways, and it is certainly not the best one. Just wanted to show it to add one entry to the TMTOWTDI bag...

    PS: even more obscurely:

    $"=",\n"; print "(@{[map {chomp; qq|'$_'|} <>]})\n";