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

Untouchable Monks. Greetings.

From the following code
while ($odbc->FetchRow()) { my @hydra_data = $odbc->Data; for my $data (@hydra_data) { if ($data) { #print $data->{source_mail}; my @db_info = split (/\s/, $data); printf "%s ",$db_info[0]; } } print "\n-----------------------------------------\n"; }
The data retrieved by $odbc->FetchRow() which is stored in @hydra_data doen’t seem to have spaces between the individual fields, so the my @db_info = split (/\s/, $data); can’t split the retrieved data into individual fields!In other words the line  printf "%s ",$db_info[0]; would print the data with no spaces between them. i.e. if the data was AAA, BBB, CCC the output is AAABBBCCC.

My question is; how can I split the data so I can get to the individual fields?

Is there something wrong with the script .i.e, Have I messed it up again somewhere? Because I thought I was doing the correct things there! Anyway, Thanks for your help and advise.

Here is the full code just in case;
#! c:/perl/bin/perl.exe -slw # $|++; require 5.008; use strict; use warnings 'all'; use Win32; use Win32::ODBC; my $dsn = "Hydra_FE"; my $qry = "qrywinbuild"; my $db = new Win32::ODBC($dsn) or die "\nError=>\t$^E : $!\n"; if (! $db) { print "\nError: $^E, ". Win32::ODBC::Error()."\n"; exit 1; } else { if ($db->sql("SELECT source_mail,hostname,machine_name FROM $qry W +HERE Business_Unit_name = '$ARGV[0]'")) { print "Error " . $db->Error() . "\n"; } else { print "\n\nObtaining $qry data from $dsn database\n\n"; while ($db->FetchRow()) { my @hydra_data = $db->Data; for my $data (@hydra_data) { if ($data) { my @db_info = split (/ /, $data); printf "%s ",$db_info[0]; } } print "\n-----------------------------------------\n"; } } } $db->Close();
Thanks guys

Blackadder

Replies are listed 'Best First'.
Re: $ODBC->FetchRow() question?
by dragonchild (Archbishop) on Dec 06, 2004 at 15:22 UTC
    You're expecting the first column to be a space-delimited bunch of data? I'll bet that if you did something like:
    use Data::Dumper; print Dumper @hydra_data;

    You'd see something a little different than what you're expecting.

    Also, use placeholders, if ODBC supports them. That way, you don't have quoting issues or SQL injection attacks. But, Win32::ODBC may not support them.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: $ODBC->FetchRow() question?
by punch_card_don (Curate) on Dec 06, 2004 at 15:59 UTC
    Do I understand that your database has multiple columns and your query was a multiple-column query, like "SELECT source_mail,hostname,machine_name FROM ..."?
    If that's the case then,
    $db->FetchRow()
    is just returning one single value from the columns returned and using
    @hydra_data ->FetchRow_Array()
    instead will put each column's value in a separate element of @hydra_data, then you won't have to split yourself.
      Thanks Brother,...This worked very well and I have learnt something new.

      Blackadder