Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Value of a query is returning without any delimiter

by parthodas (Acolyte)
on Aug 28, 2015 at 07:27 UTC ( [id://1140293]=perlquestion: print w/replies, xml ) Need Help??

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

I need to get the output of a query in a file. It is running fine. Though when I open the file, I see there is no delimiter between two values. Please help me in rectifying this.
open my $OUT, '>>', "$mycrdir/file_detail.txt" or die "Can't creat +e '$mycrdir/file_detail.txt'" ; while (my @row = $sth->fetchrow_array) { print $OUT @row, "\n"; } close $OUT;

The output looks something like this --
117365theme-mb-structure-override.css$/Compass/CodeFreeze/SIEBEL 117365login.css$/Compass/CodeFreeze/SIEBEL

Am expecting a delimiter(a space or a comma). Something like this --
117365 theme-mb-structure-override.css $/Compass/CodeFreeze/SIEBEL 117365 login.css $/Compass/CodeFreeze/SIEBEL

Replies are listed 'Best First'.
Re: Value of a query is returning without any delimiter
by Corion (Patriarch) on Aug 28, 2015 at 07:33 UTC

    If you want a delimiter, you need to print the delimiter between your elements.

    print join( ",", @row), "\n";

    Also take a look at Text::CSV_XS for creating CSV and Querylet if your program is mostly an SQL statement that should produce CSV or XLS.

      As described here:

      csv (out => ""$mycrdir/file_detail.csv", in => sub { $sth->fetch }, qu +ote_empty => 1); csv (out => ""$mycrdir/file_detail.txt", in => sub { $sth->fetch }, qu +ote_empty => 1, sep => "|"); # or " " for space

      Enjoy, Have FUN! H.Merijn
Re: Value of a query is returning without any delimiter
by Athanasius (Archbishop) on Aug 28, 2015 at 07:34 UTC

    Hello parthodas, and welcome to the Monastery!

    The special variable $, (also called $OUTPUT_FIELD_SEPARATOR) contains the delimiter you’re looking for:

    1:46 >perl -wE "my @c = qw(x y z); say @c;" xyz 17:29 >perl -wE "my @c = qw(x y z); $, = '|'; say @c;" x|y|z 17:30 >

    See perlvar.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Value of a query is returning without any delimiter
by Laurent_R (Canon) on Aug 28, 2015 at 07:53 UTC
    Depending on your database, the separators may also be inserted into the output by the dB engine with a tailored select query. For example, under Oracle:
    select CUSTOMER||';'||STATUS||';'||ID from ... where ...
    The syntax is not really nice and I would tend prefer to reformat with Perl, but it works and can be useful at least for simple queries.

    Update: Removed the delimiter after the last column.

Re: Value of a query is returning without any delimiter
by shadowsong (Pilgrim) on Aug 28, 2015 at 12:53 UTC

    change your print statement to

    print $OUT "@row\n";

    OR set the default "output field separator" explicitly.

    You may overwrite it like so

    $, = ','; print $OUT @row, "\n";
      The solution worked. Thanks a lot.
Re: Value of a query is returning without any delimiter
by Myrddin Wyllt (Hermit) on Aug 28, 2015 at 13:08 UTC

    If you just want a space as a delimiter, the easiest way is to stick the array inside the double quotes and let interpolation take care of it:

    open my $OUT, '>>', "$mycrdir/file_detail.txt" or die "Can't create '$ +mycrdir/file_detail.txt'" ; while (my @row = $sth->fetchrow_array) { print $OUT "@row\n"; } close $OUT;

    If you need a comma or anything more exotic, use join as described elsewhere in the thread

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1140293]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-16 17:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found