Gossamer Forum
Home : General : Perl Programming :

array of arrays

Quote Reply
array of arrays
I’m working on a DBman project, and have set up 3 SELECT field dropdowns, which display at the top of the search results page, which give users the opportunity to refine there search by State, Region and City.
This works fine, except that it does 2 searches of the main DB, one for the main search results and one for the dropdowns.
So I decided to integrate the dropdown code into DB.CGI sub query, so it will load the arrays used to generate the dropdowns and the search results with the one search.
This is part of sub query from DB.CGI which will explain whats going on.

open (DB, "<$db_file_name") or &cgierr("error in search. unable to open database: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB>) {
(/^#/) and next LINE; # Skip comment Lines.
(/^\s*$/) and next LINE; # Skip blank lines.
$line = $_; chomp ($line); # Remove trailing new line.
@values = &split_decode($line);

if (!(grep $_ eq $values[1], @states)) {
push (@states, $values[1]);
}
if ($values[1] eq $in{'State'}) {
if (!(grep $_ eq $values[2], @regions)) {
push (@regions, $values[2]);
}
}
if (($values[1] eq $in{'State'}) || ($values[2] eq $in{'Region'})) {
if (!(grep $_ eq $values[3], @cities)) {
push (@cities, $values[3]);
}
}
.
.
(code)
.
.
return ("ok", @hits);

The main search results are returned via @hits to:
sub view_records {
# --------------------------------------------------------
# This is called when a user is searching the database for
# viewing. All the work is done in query() and the routines just
# checks to see if the search was successful or not and returns
# the user to the appropriate page.
my ($status, @hits) = &query("view");
if ($status eq "ok") {
&html_view_success(@hits);
}
else {
&html_view_failure($status);
}
}

and then to sub html_view_success in the HTML.PL thus:
my (@hits) = @_;

What I need to do is return @hits @states @regions and @cities back to sub html_view_success, and extract them.
To do this I need to build an Array of Arrays or List of Lists and return that.
I have done a lot of reading on REFERENCES, and I understand the concept, but I,m really not to sure how to implement it.
Can some one please show me how to return multiple arrays, and extract them as I really can not work this out.

Thanks

Bob
http://totallyfreeads.com



Quote Reply
Re: array of arrays In reply to
If you want the contents of one or more arrays added to a main one use;

@arrayname = (@array1,@array2,@array3);

or something like that!!!! Smile

Hope this helps

Andy

http://www.ace-installer.com
webmaster@Ace-installer.com
Quote Reply
Re: array of arrays In reply to
I'm not sure I fully get what you're asking. Is something like this what you need?

Code:
my %stuff = (
hits => [12, 432, 53, 634],
states => ['AL', 'AR', 'AZ', 'CA', 'CO'],
regions => ['North', 'South', 'East', 'West'],
cities => ['Boston', 'Chicago', 'Los Angeles', 'Phoenix'],
);

print "State = $_\n" foreach @{$stuff{states}};
--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: array of arrays In reply to
How about this.......


my(@a,@b,@c,@d);
@a=(a,b,c,d);
@b=(1,2,3,4);
@c=(a,b,c,d);
@d=(1,2,3,4);

return(@a,@b,@c,@d);

Not quite sure what he was asking though.......



Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: array of arrays In reply to
That won't work. The array will be consolidated, if not passed as references, then dereferenced:

Code:
my (@w, @x, @y, @z) = test();

print "Result is @w\n";

sub test {
my(@a,@b,@c,@d);
@a=(a,b,c,d);
@b=(1,2,3,4);
@c=(a,b,c,d);
@d=(1,2,3,4);
return(@a,@b,@c,@d);
}
Output:

Result is a b c d 1 2 3 4 a b c d 1 2 3 4

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: array of arrays In reply to
whoooooooosh.....over my head.

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Post deleted by junko In reply to
Quote Reply
Re: array of arrays In reply to
Hey junko get out of my post.
If you need help with somehing then ask for it elsewhere.
I was very tempted to put a link in your post back to mine.

Anyway, I’ve gotten a little further with this but not much.
This is what I have.
To load up the array of arrays and call html_view_success directly -
@all_results = (\@hits,\@states,\@regions,\@states);
html_view_success(@all_results);

It says in the docs I have been reading that you should be able to access @all_results like any multi dimensional array, but if I try accessing it within html_view_success like -
my(@hits) = @_[0];
my(@states) = @_[1];
then using -
print("The first array is @_[0]\n");
or
print("The first array is @{hits} .\n");
then all I can get is the array reference ie: ARRAY(0x690f78)

I have been able to print out the contents of the arrays like this -
my(@a) = shift @_;
my(@b) = shift @_;
for $aref ( @a ) {
print "\t [ @$aref ],\n";
}
for $aref ( @b ) {
print "\t [ @$aref ],\n";
}
although It print with [ brackets ] around the array contents.
Anyway I don’t need to print them out I need to extract the arrays back to there original form so I can process then.
Help!!!

thanks
Bob
http://totallyfreeads.com

Quote Reply
Re: array of arrays In reply to
Code:
my @hits = (12, 432, 53, 634);
my @states = ('AL', 'AR', 'AZ', 'CA', 'CO');
my @regions = ('North', 'South', 'East', 'West');
my @cities = ('Boston', 'Chicago', 'Los Angeles', 'Phoenix');

@all_results = (\@hits,\@states,\@regions,\@cities);

html_view_success(@all_results);

sub html_view_success {
my @hits = @{$_[0]};
my @states = @{$_[1]};
my @regions = @{$_[2]};
my @cities = @{$_[3]};

print "hits: @hits\n";
print "states: @states\n";
print "regions: @regions\n";
print "cities: @cities\n";
}
--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: array of arrays In reply to
Mark,
thanks for your help. I now have this working.
The correct dereferencing code did the trick.

Thanks again

Bob
http://totallyfreeads.com