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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
foreach using two arrays

 



mwaf
Deleted

Jun 25, 2000, 11:11 PM

Post #1 of 10 (740 views)
foreach using two arrays Can't Post

My head doesn't seem to be working but I can't get this to work.

I have two arrays:
@title = ('Main','whatever','asdf','Links');
@url = ('index.html','whatever/index.html','asdf.html','links.html');

and I want to use for each to create a table

foreach (something) { print "<td><a href=$url>$title</a></td>"; }

If I put @title or @url instad of 'something'
it won't show both correctly and if I put @title, @url it'll make a double amount of non-working stuff.


Kanji
User / Moderator

Jun 25, 2000, 11:21 PM

Post #2 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

This'd be easier with a hash, but ...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

for my $no ( 0 .. $#title ) {
print "$title[$no] $url[$no]\n";
}</pre><HR></BLOCKQUOTE>


mwaf
Deleted

Jun 25, 2000, 11:34 PM

Post #3 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

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

syntax error at D:\Perl\bin\menu.pl line 11, near "$no ("</pre><HR></BLOCKQUOTE>

is there something I should have changed?
also, it would be nice if you'd explain to me what this actually does...


Kanji
User / Moderator

Jun 26, 2000, 12:09 AM

Post #4 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

Works fine for me under both Win32 (5.005_03, ActiveState build 522) and UNIX, so the problem is likely to be somewhere between lines 1 and 10. :-)

More than likely, you forgot a semi-colon ( ; ) at the end of line 10 so that perl thinks you're trying to do something wacky.

As for what it does ...

$#title returns the subscript of the last element in @title, so we know how long the array is (3 in this instance, because array subscripts count from 0).

So we now have for ( 0 .. 3 ), which is the same as for ( 0, 1, 2, 3 ) as the range operator (..) returns an array of all values between and inclusive of the values preceeding and following it.

As the code iterates over the values returned by .., it uses the number returned as a subscript on the two arrays you want to pull info from.

It's a nice compact way of saying ...

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

print "$title[0] $url[0]\n";
print "$title[1] $url[1]\n";
print "$title[2] $url[2]\n";
print "$title[3] $url[3]\n";</pre><HR></BLOCKQUOTE>

Actually, I just thought of something: if @title/@url are likely to get very large, you can save memory usage (because you're not creating a huge list of all values between 0 and however big the array is) by doing ...

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

my $no = 0;
while( exists $title[$no] ) {
print "$title[$no] $url[$no]\n";
$no++; # $no + 1
}</pre><HR></BLOCKQUOTE>

In this case, exists() returns true if $title[$no] exists (because 0 or "" may be valid values, if they weren't you could leave exists() out), and performs the code inside the loop.

[This message has been edited by Kanji (edited 06-26-2000).]


mwaf
Deleted

Jun 26, 2000, 1:01 AM

Post #5 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

Thanks! (once again)

I tried both as you suggested in another post (even though the first one worked) and they both worked.

Thanks also for explaining what happens, it's much nicer when you know what your scripts do, therefor I never use other peoples 'just customize this' scripts.


japhy
Enthusiast

Jun 26, 2000, 6:57 AM

Post #6 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

Kanji - exists() is a hash function -- you were thinking of the defined() function.

I would approach the problem in one of several ways:

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


for (0 .. $#URLs) {
($url,$title) = ($URL[$_],$TITLE[$_]);
# ...
}

# or

for (my $i = 0; ($url,$title) = ($URL[$i],$TITLE[$i]); $i++) {
# ...
}

# or

# totally different -- uses one data structure
# to hold the information as an array reference
for (@structure) {
($url,$title) = @$_;
# ...
}
</pre><HR></BLOCKQUOTE>


DrZed
User

Jun 26, 2000, 8:07 AM

Post #7 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

Just to point out what the first response mentioned, the nature of the data suggests the use of a hash would be better than two arrays.

Instead of:
@title = ('Main','whatever','asdf','Links');
@url = ('index.html','whatever/index.html','asdf.html','links.html');

You should probably use:
%url = ( Main => 'index.html',
whatever => 'whatever/index.html',
asdf => 'asdf.html',
links => 'links.html');

Instead of:
foreach (something) { print "<td><a href=$url>$title</a></td>"; }

You would be able to do the following:

foreach (keys %url) { print "<td><a href=$url{$_}>$_</a></td>";

Dr. Zed


Kanji
User / Moderator

Jun 26, 2000, 8:49 AM

Post #8 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

 <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>japhy did saided:
Kanji - exists() is a hash function -- you were thinking of the defined() function.<HR></BLOCKQUOTE>

Gah, major brain fart there and thanks for the catch.

That'll teach me to post after 27hrs of consciousness ... :-)


mwaf
Deleted

Jun 27, 2000, 1:44 AM

Post #9 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

Thanks DrZed, works fine...

however, I kept the old one as I use the array info for other stuff too.


Kanji
User / Moderator

Jun 27, 2000, 4:35 AM

Post #10 of 10 (740 views)
Re: foreach using two arrays [In reply to] Can't Post

 <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>japhy did saided:
Kanji - exists() is a hash function -- you were thinking of the defined() function.<HR></BLOCKQUOTE>

Now that I've slept some ( :^) ), it turns out I wasn't blowing it out my stack: exists() does work on arrays in 5.6.0 (which I use).

defined() is the better choice, 'though: 5.005 is still far too common and prolly will be for a long time to come.

 
 


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

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