Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to get this not the usual round robin looping

by hdb (Monsignor)
on Oct 13, 2015 at 08:22 UTC ( [id://1144647]=note: print w/replies, xml ) Need Help??


in reply to How to get this not the usual round robin looping

My humble solution:

use strict; use warnings; use Data::Dumper; my @K = qw (H1 H2 H3 H4); my @V = qw (1 2 3 4 5 6 7 8 9 10); my %hash; my $i = -1; push @{ $hash{ $K[ ++$i&4 ? 3-$i%4 : $i%4 ] } }, $_ for @V; print Dumper \%hash;

UPDATE: ...or...

push @{ $hash{ $K[ int 3.5*abs sin (0.19634954085+0.39269908170*++$i) +] } }, $_ for @V;

Replies are listed 'Best First'.
Re^2: How to get this not the usual round robin looping
by Discipulus (Canon) on Oct 14, 2015 at 07:48 UTC
    chapeau for your solutions, but while i was reading them to appreciate how bad i was to not studied math (quite at all), i realized that your two push lines work only for 0 to 3. has your math solution the possibility to abstract this and using the index of a variable @K?

    thanks
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Well spotted! Right now I cannot think of something as terse as this for the general solution. The $i&4 basically tells the ups and downs and would need to be replaced by something like int($i/@K)&1 (not tested). The line based on sin will not work for large arrays @K as sin is not linear enough for that...

      Update: This should work:

      push @{ $hash{ $K[ int(++$i/@K)&1 ? @K-1-$i%@K : $i%@K ] } }, $_ for @ +V;
Re^2: How to get this not the usual round robin looping
by karlgoethebier (Abbot) on Oct 13, 2015 at 17:05 UTC
    "My humble solution"

    I guess this a typical case of total underestimation.

    Update: What a cool solution!

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      You are exaggerating!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-24 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found