Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Formalizing an array type problem, looking for solution

by GrandFather (Saint)
on Sep 02, 2016 at 01:41 UTC ( [id://1171028]=note: print w/replies, xml ) Need Help??


in reply to Re: Formalizing an array type problem, looking for solution
in thread Formalizing an array type problem, looking for solution

I've no idea what a "consensus path" is so the following code, while fun to write, is probably wildly wide of what you want to achieve. It may act as a starting point for discussion though.

#!/usr/bin/perl -l use strict; use warnings; my @paths; push @paths, [map {chomp; $_} split /\s*:\s*/] while <DATA>; my @pattern = @{pop @paths}; for my $part (@pattern) { my ($node, $dir) = split '', $part; for my $testPattern (@paths) { if ($dir eq '+') { next if $testPattern->[0] !~ $node; $part = $testPattern; last; } else { next if $testPattern->[-1] !~ $node; $part = [map {tr~+-~-+~; $_} reverse @$testPattern]; last; } } die "Can't match $part in '@pattern'\n" if 'ARRAY' ne ref $part; } print join ' : ', map {@$_} @pattern; __DATA__ A+ : B+ C+ : D- : E- C+ : E-

Prints:

C- : D+ : E+ : E+ : D+ : C-
Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^3: Formalizing an array type problem, looking for solution
by Anonymous Monk on Sep 02, 2016 at 21:08 UTC

    I think the OP means a consensus path is a join of all the segments where the segments are joined by overlaying identical elements (thus deleting one) at the joining ends. By this rule the two valid "consensus paths" are

    A+ : B+ : E+ : D+ : C- C+ : D- : E- : B- : A-

    which are the reverse (and sign change) of each other.

    I do wish the OP would speak up because I have a sneaking suspicion his real data is not all single letters, and so my solution Re: Formalizing an array type problem, looking for solution above will need "tweaking".

      I replied to your code because I assumed it was the OP who'd forgotten to log on. Yes, it'd be nice if the OP would follow up some of the questions and suggestions.

      Premature optimization is the root of all job security

Log In?
Username:
Password:

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

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

    No recent polls found