I am trying to write a program to do the following. I have a list of links. Each link is identified by a start and finish end and I need the program to iterate through the lis and join the links up. For example the links are stored in a hash like this.
my %T1 = ( 'a|b' => 'a|b', 'b|c' => 'b|c', 'c|d' => 'c|d', 'j|k' => 'j|k', 'k|l' => 'k|l', 'l|m' => 'l|m', 'm|n' => 'm|n' );
and I want the links to be joined as a|b|c|d and j|k|l|m|n i.ie in my code below I want my %newTrailsToCombine hash to look like
my %newTrailsToCombine = ( 'a|b' => 'a|b|c|d', 'b|c' => 'a|b|c|d', 'c|d' => 'a|b|c|d', 'j|k' => 'j|k|l|m|n', 'k|l' => 'j|k|l|m|n', 'l|m' => 'j|k|l|m|n', 'm|n' => 'j|k|l|m|n' );
instead it looks like
%newTrailsToCombine$VAR1 = { 'm|n' => 'm|n|l|m', 'j|k' => 'j|k|k|l', 'k|l' => 'k|l|j|k|l|m', 'b|c' => 'b|c|a|b|c|d', 'a|b' => 'a|b|b|c', 'l|m' => 'l|m|k|l|m|n', 'c|d' => 'c|d|b|c'
Below is my code , any help/ideas would be appreciated Thanks Phil
#! /opt/perl/bin/perl -w use strict; use Data::Dumper; my %T1 = ( 'a|b' => 'a|b', 'b|c' => 'b|c', 'c|d' => 'c|d', 'j|k' => 'j|k', 'k|l' => 'k|l', 'l|m' => 'l|m', 'm|n' => 'm|n' ); my %trailsToCombine = %T1; my %newTrailsToCombine; my %clone_trailsToCombine; my $seenFirst; my %seen; %clone_trailsToCombine = %trailsToCombine; while (my($K, $V) = each (%trailsToCombine)) { my @Karr = split (/\|/,$V); my $tmp = $K; foreach my $i (@Karr) { while (my($SK, $SV) = each (%clone_trailsToCombine)) { if( $K eq $SK) { #ignore } elsif((my $count = grep /$i/,$SK) >= 1 ) { $tmp = $tmp."|$SK"; } else { #no match } } } if( ! exists $newTrailsToCombine{$K}) { $newTrailsToCombine{$K} = $tmp; } } #process_trailsToCombine(%trailsToCombine); print "\n Dumping \%newTrailsToCombine"; print Dumper(\%newTrailsToCombine);

In reply to Link Connectivity Algorithim by dunkirk_phil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.