Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Iterating through two arrays at once
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jonathan Scott Duff  
View profile  
 More options Dec 11 2003, 7:49 am
Newsgroups: perl.perl6.language
From: d...@lighthouse.tamucc.edu (Jonathan Scott Duff)
Date: Wed, 10 Dec 2003 23:02:16 -0600
Local: Thurs, Dec 11 2003 12:02 am
Subject: Re: Iterating through two arrays at once

On Wed, Dec 10, 2003 at 11:44:15PM -0500, Joe Gottman wrote:
>    In Perl 6, how will it be possible to iterate through two arrays at the
> same time?  According to Apocalypse 4,  the syntax is
>     for @a; @b -> $a; $b {

> According to the book  "Perl 6 Essentials" the syntax is
>     for zip(@a, @b) -> $a, $b {

> Which of these is right? (of course, this being Perl, both may be right).

FWIW, I like the former even though the latter has lots of precedent
in other languages.

> Whichever of these syntaxes is right, what happens when @a and @b are
> of different sizes? I can think of three possible behaviors, each with
> its potential drawbacks:

>     1) The loop executes min(+ @a, + @b) times, then finishes
>        successfully.
>     2) The loop executes min(+ @a, + @b) times, then throws an
>        exception because the arrays were not of the same size.
>     3) The loop executes max(+ @a, + @b) times. If @a has fewer
>        elements than @b, then after @a's elements are exhausted $a is
>        set to undef, and similarly if @b has fewer elements than @a.

> In cases 1) and 2), the problem is how to get the elements of the
> larger array that were never iterated over. Case 2) is probably better
> than case 1), because the exception that is thrown might contain
> information about which array was larger and which elements of it have
> yet to be examined. In case 3), the problem is differentiating between
> an undef returned because the arrays were of different sizes, and an
> undef returned because one of the arrays contained an undef.

I believe that case 3) is the "right" answer.  Why do you need to
differentiate the undefs?  If you cared about whether one array was
bigger than the other, surely you could check that yourself.  In any
case, run-time properties (is this redundant?) can help you out.
Perhaps you get an "undef but out_of_bounds" kind of value back when
you run off the end of the shorter array.

-Scott
--
Jonathan Scott Duff
d...@lighthouse.tamucc.edu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.