Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: reordering segments to form a polygon

by fergal (Chaplain)
on Aug 13, 2004 at 16:52 UTC ( #382748=note: print w/replies, xml ) Need Help??


in reply to reordering segments to form a polygon

Here's a version that only requires a single pass over the array. The end result is that the 5th element of each segment is a ref to the next one. The last one doesn't reference the next or we'd get a circular structure.
use warnings; use strict; my @seg = ( [ 1, 1, 2, 1 ], [ 5, 5, 1, 5 ], [ 2, 3, 5, 3 ], [ 5, 3, 5, 5 ], [ 2, 1, 2, 3 ], [ 1, 5, 1, 1 ] ); use Data::Dumper qw(Dumper); my (%have, %want); while (my $seg = shift @seg) { my $have = join(",", @{$seg}[0,1]); my $want = join(",", @{$seg}[2,3]); print "we have $have\n"; if (my $wanted = delete $want{$have}) { print "something wants it\n"; $wanted->[4] = $seg; } else { print "nothing wants it yet\n"; $have{$have} = $seg; } last if @seg == 0; # don't want to make a circular reference print "we want $want\n"; if (my $had = delete $have{$want}) { print "found it\n"; $seg->[4] = $had; } else { print "didn't find it, asking for it\n"; $want{$want} = $seg; } } $Data::Dumper::Indent = 0; print Dumper values %have;
the end result is
[1,1,2,1,[2,1,2,3,[2,3,5,3,[5,3,5,5,[5,5,1,5,[1,5,1,1]]]]]]
It's glorious day in Dublin so I'm off to the pub now :-)

UpdateI suppose it does require a second pass to produce output in the format requested...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2022-12-05 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?