Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: mashing two arrays

by thospel (Hermit)
on Jan 20, 2007 at 22:17 UTC ( [id://595699]=note: print w/replies, xml ) Need Help??


in reply to mashing two arrays

If the arrays are already sorted, it might be a pity to lose that information and have to re-sort at the end though. You might consider doing a standard merge instead:
my @baz; my $f = $b = 0; while ($f < @foo && $b < @bar) { my $cmp = $foo[$f][0] cmp $bar[$b][0]; push @baz, [$cmp <= 0 ? @{$foo[$f++]} : (@{$bar[$b]}[0, 1], undef), $cmp >= 0 ? $bar[$b++][-1] : undef]; } push @baz, [@{$foo[$_]}, undef] for $f..$#foo; push @baz, [@{$bar[$_]}[0, 1], undef, $bar[$_][-1]] for $b..$#bar;
You can do some time measurements with your real data to see if this gains you anything or not.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://595699]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-18 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found