Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey, This thing..(my browser) keep me logging out..? Why?
Hi, Here is the code snippet:
Given @a and @b, you have to get @c
I am looking for better method and faster method;
Also, If anyone can think of more general formula, that would be helpful to combine multiple array.
The numbers in the arrays are just place holders
It could be @a = qw(1.2.3 4.5.6 1.2 1.7.9 2) etc..
In other words, simple sorting would not work
Thanks, Artist
#!/usr/bin/perl @a = (3,5,2,6); @b = (7,3,6); map { $x{$_} = -1 } @a; map { $x{$_} += 1 } @b; foreach (sort keys %x){ $val = $x{$_}; if(!$val) { print "$_:$_"; } elsif($val > 0){ print "-:$_"} else{ print "$_:-"}; print "\n"; } #@c = ( [2,'-'],[3,3],[5,'-'],[6,6],[7,'-']);

Replies are listed 'Best First'.
Re: Merging the seperated
by MeowChow (Vicar) on Jul 04, 2001 at 10:21 UTC
    Even though it sounds like homework (summer school?) ... here's a general solution for any number of arrays:
    sub merge { my %h; for my $l (0..$#_) { $h{$_}[$l] = $_ for @{$_[$l]}; } map [ map { defined $_ ? $_ : '-' } @{$h{$_}}[0..$#_] ], sort { $a <=> $b } keys %h; } ### Example ### use strict; use Data::Dumper; my @h = merge [3,5,2,6], [7,3,6], [1,6,2,9], [0,3,4,1]; print Dumper \@h;
    Your code could be cleaner, but algorithmically, you've got the right idea.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print