Gossamer Forum
Home : General : Perl Programming :

Array problem!

Quote Reply
Array problem!
Argh..this is annoying me! What am I doing wrong?

my @banner_urls = qw(

1 => $opts->{'Banner_1'}
2 => $opts->{'Banner_2'}
3 => $opts->{'Banner_3'}
4 => $opts->{'Banner_4'}
5 => $opts->{'Banner_5'}
6 => $opts->{'Banner_6'}
7 => $opts->{'Banner_7'}
8 => $opts->{'Banner_8'}
9 => $opts->{'Banner_9'}
10 => $opts->{'Banner_10'}

);

When calling it via $banner_urls[5], for example, it just prints $opts->{'Banner_5'}

Is there something really basic I'm missing?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Array problem! In reply to
what exactly are you trying to do? an array of hashes? array of arrays? what context is the variable being used in?

--Philip
Links 2.0 moderator
Quote Reply
Re: [sponge] Array problem! In reply to
I am getting a random number, from 1 to 10, and then trying to grab that number from the array. The variable for Links SQL is supposed to be held in the array.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Array problem! In reply to
You'll probably be kicking yourself after this: You probably don't want to use qw(. It basically does a split on whitespaces and doesn't interpolate.

So instead:

Code:
my @banner_urls = (

1 => $opts->{'Banner_1'},
2 => $opts->{'Banner_2'},
3 => $opts->{'Banner_3'},
4 => $opts->{'Banner_4'},
5 => $opts->{'Banner_5'},
6 => $opts->{'Banner_6'},
7 => $opts->{'Banner_7'},
8 => $opts->{'Banner_8'},
9 => $opts->{'Banner_9'},
10 => $opts->{'Banner_10'},
);

Which incidentally can be shorted to something this:

Code:
my @banner_urls = map {( $_ => $opts->{'Banner_' . $_} )} ( 1..10 );
Code:

Though another question would be, why are you setting up like it was a hash?
Quote Reply
Re: [Andy] Array problem! In reply to
Code:
my $rand = rand(10);
my $rand_url = $opts->{Banner_$rand};

Last edited by:

Paul: Aug 14, 2002, 11:40 AM