views:

130

answers:

5

Hello,

this is how my table looks:

key | driver  | machine     | result
-----------------------------------
1   | 1234    | abc_machine | pass
2   | 1234    | xyz_machine | fail

when a user selects '1234' from driver and all from machine things get a little messy. (user makes selection from a gui)

when i do:

$getConfig = `sqlite3 abc.db "SELECT machine FROM $table_name WHERE driver='$drvrSel'"`;

it gives me abc_machine xyz_machine.

I tried separating them using split(/ /, $getConfig), but does not work.

Thank you.

I am sorry if i asked this question in a wrong place. I tried finding right place but couldn't find it.

A: 

There might be a hidden tab or newline character in there. Look at the ascii value of the empty character between the strings and use that in your split call.

Jesse
+2  A: 

Instead of calling the SQLite program, you should use DBI.

By the way:

perl -MData::Dumper -e '$_="abc_machine xyz_machine";print Dumper split / /;'

$VAR1 = 'abc_machine';
$VAR2 = 'xyz_machine';

So, check the data contained in $getConfig again.

Alan Haggai Alavi
`s/you can/you *really, really* should/`
Telemachus
Updated answer. Thank you.
Alan Haggai Alavi
A: 

Actually sqlite3 supports two command-line options that might be useful to you: -list (the separator is '|') and -csv for csv file. See the man page for sqlite3 for details. You can then split on the separator or use Text::CSV_XS to separate the individual fields

Following Rashids comment below, I had clearly misunderstood your question. My mistake. Here is one way to do this:

my $result = `sqlite3 dbname "query"`;
$result =~ s/\n/ /gs;
Gurunandan
well the separator is already out of the way.
rashid
+5  A: 
#!/usr/bin/perl

use DBI;
use strict;

my $dbargs = {AutoCommit => 0, PrintError => 1};

my $dbh = DBI->connect("dbi:SQLite:dbname=test.db","","",$dbargs);
my $sth = $dbh->prepare("SELECT machine FROM test WHERE driver = 1234");
$sth->execute();

my @data;

while (@data = $sth->fetchrow_array()) {
    print $data[0] . "\n";
}

$sth->finish;
$dbh->disconnect;
Can Berk Güder
Good answer, but unnecessary use of finish() since the driver automatically calls it for you when you fetch the last row from the result set.
converter42
oh. thanks for the correction, I haven't written Perl for quite some time.
Can Berk Güder
Calling finish is a good practice, even if it's unnecessary in a particular case. If you always call it, you won't forget to call it when you need to. If you don't always call it, you might forget to call it when you need to. :)
brian d foy
A: 

ok guys i found it...

\W matches any non-“word” character

`@setConfig = split(/\W/, $getConfig);`

this separates it nicely into:

$setConfig[0] = abc_machine $setConfig[1] = xyz_machine

thanks everyone.

rashid
You asked the wrong question, so you are focusing on the wrong answer. You should be using DBI to programmatically get your data, *not* calling a shell program.
Ether
@Ether: yes. A thousand times, yes. I feel a bit like screaming when this kind of post comes along. I'm going to go meditate now, so that I don't pull a bobince...(cite: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)
Telemachus
Don't be fooled by that \W. There are lots of characters that aren't in \W (like a literal .). Perhaps you wanted to split on something like whitespace instead.
brian d foy
sure guys i asked the wrong question, but i was really frustrated and had alot of pressure but thanks its over now. I think i needed meditation.
rashid