CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
An array question.



Sep 19, 2000, 12:50 AM

Post #1 of 5 (3154 views)
An array question. Can't Post

Ok this is confusing,

Here is what I need to do,

I have an array, I put my data in the array by doing this,

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

push(@alts, ucfirst($in{'0'}), $in{'url'});
push(@alts, ucfirst($in{'1'}), $in{'1url'});
push(@alts, ucfirst($in{'2'}), $in{'2url'});
push(@alts, ucfirst($in{'3'}), $in{'3url'});
push(@alts, ucfirst($in{'4'}), $in{'4url'});
push(@alts, ucfirst($in{'5'}), $in{'5url'});
push(@alts, ucfirst($in{'6'}), $in{'6url'});
push(@alts, ucfirst($in{'7'}), $in{'7url'});
push(@alts, ucfirst($in{'8'}), $in{'8url'});
push(@alts, ucfirst($in{'9'}), $in{'9url'});


until $in{'url50'}

Now I need to loop through the data and preform a regex on the $in{'9url'} part, but not the $in{'9'}. Basically, How do I handle the data in column 2 differently than in column one. Then print out the data. I got the printint part down.

This is hard for me to even explain. Do you see what I'm trying to do here?

I'll give an example.

The url is "" and the link is called "click here"

I would loop through the data and add the string "" before the url. As for the link name I would just print it as is.

Now can you picture what I'm trying to do?

It's not exactly what I need to do but it's a good example. What I'm really doing is very much more confusing and that's why I used an example different than what I'm doing.

I hope you can help me out,

Is this a multidimensional array that I'm talking about?




Sep 18, 2000, 10:57 PM

Post #2 of 5 (3154 views)
Re: An array question. [In reply to] Can't Post

Thanks Kanji,

I would have never though of skipping all the even numbers.

Thanks A lot, Smile Smile


User / Moderator

Sep 19, 2000, 5:54 AM

Post #3 of 5 (3154 views)
Re: An array question. [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for $idx ( 0 .. $#alts ) {
next unless $idx % 2; # skips odd indexes
$alts[$idx] = "http://.../?$alts[$idx]";

... and, no, that's not a multi-dimensional array which would look more like ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

push @alts, [ $in{"0"}, $in{"url"} ];
for $url ( @alts ) {
# $url is an array ref
# ->[1] is its second element
$url->[1] = "http://.../?$url->[1]";

User / Moderator

Sep 19, 2000, 11:39 AM

Post #4 of 5 (3154 views)
Re: An array question. [In reply to] Can't Post

You can also save yourself by adding it in at generation time ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

push(@alts, ucfirst($in{'0'}), "http://.../?url=$in{'url'}" );</pre><HR></BLOCKQUOTE>

Not sure why you're not doing it that way. Are you really doing 50 odd push() statements?

If so, you can make a (hopefully) minor change to your script and vastly simplify the process with the added benefit of easy-scaling if you decide to add or remove URLs.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for my $no ( 0 .. 50 ) {
push @alts,
ucfirst( $in{$no} ),

The change, if you can't see it, is making $in{'url'} be $in{'0url'} so you have consistency (which makes it easy to manipulate).

[This message has been edited by Kanji (edited 09-19-2000).]


Sep 22, 2000, 10:09 AM

Post #5 of 5 (3154 views)
Re: An array question. [In reply to] Can't Post

Thanks A lot for that code Kanji,

I learned a lot from that second peice of code. It also worked like a charm. It's a lot faster now that I'm not pushing all 50 of them one at a time.




Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives