--- Booher Timothy B 1stLt AFRL/MNAC <[EMAIL PROTECTED]> wrote:
> o.k. here is another one:
> 
> I still have a text file I am trying to get into a csv line, for example:
> 
>             cheese          olives         beans       carrots
> 
> So I put this into an array like:
>       
> my @headers = split /\s+/;
> 
> But now I want to print this in csv format:
> "cheese","olives","beans","carrots".
> 
> My best results have been to use:
> 
>       foreach (@headers) {
>         print "\"$_\",";
>       }

Timothy,

I'd use a proper module for something like this.  CSV data can get tricky, so see if 
you can
install Text::CSV_XS.  Here's some sample code:

    use strict;
    use Text::CSV_XS;
    
    my $csv = Text::CSV_XS->new;
    
    while (<DATA>)
    {
        my @elements = split;
        my $status   = $csv->combine( @elements );
        if ( ! $status )
        {
            print $csv->error_input;
        }
        else
        {
            print $csv->string, "\n";
        }
    }
    __DATA__
                cheese          olives         beans       carrots
             this          isn't         a       "great"   example

Note the special use of split.  In this case, we're not specifying a variable, so it 
splits on $_.
 Further, since we're not splitting on a regular expression, it automatically splits 
on whitespace
after skipping any leading whitespace.

Cheers,
Curtis "Ovid" Poe

=====
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A

__________________________________________________
Do You Yahoo!?
Send your FREE holiday greetings online!
http://greetings.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to