Gossamer Forum
Home : General : Perl Programming :

Perl Array Problem

Quote Reply
Perl Array Problem
I have an installed copy of DbMan:SQL and a Global Variable called "list_currentCarrier" (but this is more of a perl question than a DbMan question). Basically it lists a bunch of data from a database and SHOULD output a comma seperated list of that data. Here is what I have:

Code:

sub {

....






my $list = $DB->table("Insured")->select('Carrier', $cond)->fetchall_hashref;
return join(",", $list);

}


Now this is only part of the code ($Db, $Table and $cond are all correctly defind and I have verified that they work correctly) but my problem is that the array list SHOULD return the list of data but all I get back from the script is something like "ARRAY(0xa03c30c)". I believe I am doing something wrong but can't quite figure it out?

Thank you for any suggestions you may have.

Quote Reply
Re: [LanceWilson2] Perl Array Problem In reply to
Hi,

You're right. It is related to Perl. Just change the last line as below

return join(",", @$list);

That should work.

Cheers,



Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] Perl Array Problem In reply to
Not sure if we got any closer or not...???
Now it says "HASH(0x9ed4764)" instead of the database items.

Code:

my $list = $DB->table("Insured")->select('Carrier', $cond)->fetchall_hashref;
return join(",", @$list);
Quote Reply
Re: [LanceWilson2] Perl Array Problem In reply to
Eh, because you use fetchall_hashref.
So hashref is passed back.
you should use %$list.

However you can't join %$list.

So you should use fetchall_arrayRef(), then go through each row with a loop joining array of each line.

Code:
my $table= $DB->table("Insured")->select('Carrier', $cond)->fetchall_arrayRef();
my $out;
foreach my $row (@$table) {
$out .= join(",", @$row) . "\n";
}
return $out;

Not tested, but theoritically should work.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Jan 6, 2006, 11:16 AM
Quote Reply
Re: [webmaster33] Perl Array Problem In reply to
I had to do a few little modification but YES I do see where the problem was. THANK YOU! It now works.
I'll keep your screen name around for future help I may need (maybe next time I'll have a bigger project that I can pay you for)
Quote Reply
Re: [LanceWilson2] Perl Array Problem In reply to
You are welcome Smile

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Perl Array Problem In reply to
You could also do:

Code:
my @rows = $DB->table("Insured")->select('Carrier', $cond)->fetchall_list;
return join(",", @rows);

which is a little simpler. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Perl Array Problem In reply to
Yes, true, but your solution is simpler in just a specific, ideal case.
In real world cases, we likely need to print data into a table, where not just fields delimiters, but new lines are needed to be also inserted.

Your solution is nice code snippet, anyway.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [Alex] Perl Array Problem In reply to
If we would want a short solution with table-like result, then we could use this short code:
Code:
my $table= $DB->table("Insured")->select('Carrier', $cond)->fetchall_arrayRef(); # do table query
return join("\n", map{ join(', ', @$_) } @$table); # return a formatted table
However I don't really like such simplification, as it makes the code harder to read.
Perhaps the original one I posted is easier to read, altough a bit longer.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Jan 13, 2006, 7:44 PM
Quote Reply
Re: [webmaster33] Perl Array Problem In reply to
Hi,

But you can skip the map/dereference by using fetchall_list rather then fetchall_array, which makes things much easier to read. A nice trick when selecting two columns is to use fetchall_list like this:

my %id_map = $DB->table('Links')->select('ID', 'Title')->fetchall_list;

and you get a hash of ID => Title.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Perl Array Problem In reply to
Yeah, for two columns your hash based solution is pretty cool!
For the whole database table display, my 2 solutions are usable.

I aggree, for the original case, listing 1 column query into an array, your fetchall_list solution is the best solution:
Code:
my @rows = $DB->table("Insured")->select('Carrier', $cond)->fetchall_list;
return join(",", @rows);

Lance, now you have several code snippets (for GT database query) for different tasks, which you can use in your globals... Cool

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...