Perl

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Pair of lists => list of pairs?

    7 answers - 332 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Suppose I have two arrays @k and @v and I want to declare and initialize a
    hash %h such that %h.keys eqv @k and %h.values eqv @v.
    I could use a direct translation of the P5 idiom:
    my %h;
    %h{@k} = @v;
    But is there an easy way in Perl6 to do it all in one go? Should this work?
    my %h = @k [=>] @v;
  • No.1 | | 308 bytes | |

    Wed, Aug 23, 2006 at 05:43:48PM -0400, Mark J. Reed wrote:
    But is there an easy way in Perl6 to do it all in one go? Should this work?

    my %h = @k [=>] @v;

    You want a zip:

    my %h = @k @v;
    my %h = @k Y @v; # ASCII fallback
    my %h = zip(@k, @v); # or maybe zip(@k; @v) this week?
  • No.2 | | 577 bytes | |

    Wed, Aug 23, 2006 at 05:43:48PM -0400, Mark J. Reed wrote:
    : Suppose I have two arrays @k and @v and I want to declare and initialize a
    : hash %h such that %h.keys eqv @k and %h.values eqv @v.
    :
    : I could use a direct translation of the P5 idiom:
    :
    : my %h;
    : %h{@k} = @v;
    :
    : But is there an easy way in Perl6 to do it all in one go? Should this work?
    :
    : my %h = @k [=>] @v;

    Reduce operators only turn infix into list operators. What you really
    want here is a hyper-fatarrow:

    my %h = @k => @v;

    Larry
  • No.3 | | 340 bytes | |

    Mark J. Reed skribis 2006-08-23 17:43 (-0400):
    But is there an easy way in Perl6 to do it all in one go? Should this work?
    my %h = @k [=>] @v;

    Hyper is not [], but >><<. And << works perfectly in Pugs, and does
    exactly what you describe.

    [] is for reduction, and is prefix: [+] 1,2,3

    Juerd
  • No.4 | | 440 bytes | |

    >
    : my %h;
    : %h{@k} = @v;
    :
    : But is there an easy way in Perl6 to do it all in one go? Should this
    work?
    :
    : my %h = @k [=>] @v;

    Reduce operators only turn infix into list operators. What you really
    want here is a hyper-fatarrow:

    my %h = @k => @v;

    Gaal pointed out using zip. What would be the difference then between a
    hyper-fatarrow and zip in this case?

    Michael
  • No.5 | | 1087 bytes | |

    Thu, Aug 24, 2006 at 12:51:04AM +0300, Gaal Yahas wrote:
    : Wed, Aug 23, 2006 at 05:43:48PM -0400, Mark J. Reed wrote:
    : But is there an easy way in Perl6 to do it all in one go? Should this work?
    :
    : my %h = @k [=>] @v;
    :
    : You want a zip:
    :
    : my %h = @k @v;
    : my %h = @k Y @v; # ASCII fallback

    That would have worked back when zip merely interleaved, but now it makes
    sublists, and so we would have to teach hash assignment to transform [$k, $v]
    into ($k,$v) or $k=>$v. Might not be a bad idea.

    : my %h = zip(@k, @v); # or maybe zip(@k; @v) this week?

    It would be zip(@k;@v). zip(@k,@v) would only have one dimension, so
    would just concatenate the two lists and put each element into its
    own sublist.

    Alternately, for the old zip semantics we have each(@k;@v), which
    makes a list with interleaved keys and values. It's just there's no
    operator like for it (yet).

    But I'd still probably use a hyper-fatarrow for this case rather than
    relying on interleaving.

    Larry
  • No.6 | | 610 bytes | |

    >
    Reduce operators only turn infix into list operators. What you really
    want here is a hyper-fatarrow:

    my %h = @k => @v;

    Ah, right. Silly me. I got hyper and reduce confused. Thanks!

    Gaal pointed out using zip. What would be the difference then between a
    hyper-fatarrow and zip in this case?

    Effectively none. But I think the hyper-notation is clearer here. Both
    and =make pairs, but at least to me, =conveys more explicitly that it's
    not just any old pair but specifically a key/value pair. (Even though =>
    also creates Pairs of the "any old" kind.)
  • No.7 | | 645 bytes | |

    Wed, Aug 23, 2006 at 03:19:22PM -0700, Larry Wall wrote:
    : But I'd still probably use a hyper-fatarrow for this case rather than
    : relying on interleaving.

    Another reason for preferring hyper is that it makes promises about
    parallelizability, whereas the zip/each solutions would tend to
    assume the input streams must be processed in order, albeit lazily.
    (Probably doesn't make much difference until someone actually attempts
    to vectorize Perl though, and even when that happens, it's not clear
    what the exact sequence of events would be if you feed a lazy pipe
    to a hyper)

    Larry

Re: Pair of lists => list of pairs?


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 1 =
QUESTION ON "Perl"

EMSDN.COM