Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Printing in a WHILE loop

by GrandFather (Saint)
on May 28, 2014 at 07:04 UTC ( [id://1087612]=note: print w/replies, xml ) Need Help??


in reply to Printing in a WHILE loop

Or, since you used the keyword "database", how about:

use strict; use warnings; use DBI; open my $fOut, '>', "delme" or die "Can't create deleme.txt: $!\n"; print $fOut <<FILE; Line,Name,Colour,Comment 1,Fred,Blue,"Comment with a comma, and stuff." 2,Joe,Green,No comma so no need for quotes 3,Bob,Red,"""A comma, and quotes""" 4,Sam,Yellow,"Penultimate line, but split across two input lines" 5,Bill,Violet,And an ordinary line to finish FILE close $fOut; my $dbh = DBI->connect('dbi:CSV:', '', '', {f_file => 'delme'}); my $sth = $dbh->prepare("select * from delme"); $sth->execute(); while (my $row = $sth->fetchrow_hashref()) { print "$_: '$row->{$_}'\n" for sort keys %$row; print "\n"; }

Prints:

colour: 'Blue' comment: 'Comment with a comma, and stuff.' line: '1' name: 'Fred' colour: 'Green' comment: 'No comma so no need for quotes' line: '2' name: 'Joe' colour: 'Red' comment: '"A comma, and quotes"' line: '3' name: 'Bob' colour: 'Yellow' comment: 'Penultimate line, but split across two input lines' line: '4' name: 'Sam' colour: 'Violet' comment: 'And an ordinary line to finish' line: '5' name: 'Bill'

No serious intent that you should use this for your current problem, but it does go some way to show just how flexible DBI is!

Perl is the programming world's equivalent of English

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found