Thank you very much, Corion .

I did think about a loop but it seems that I needed that the pope gives me a kick :-) I must admit however that I could not wrap my head around the flag $changed yet. Could you please give me another hint on this? Below is my new attempt with the do ... while loop. I updated the sample data to see that the longer chain will be proceeded.

!/perl use strict; use warnings FATAL => qw(all); use Text::CSV_XS; use Data::Dump; my $csv_par = { binary => 1, auto_diag => 1, allow_whitespace => 1, sep_char => ';', eol => $/, quote_char => undef, }; my $csv = Text::CSV_XS->new($csv_par); my @data = @{ $csv->getline_all(*DATA) }; shift @data; # simply throw away the header. my %main; my %hash_old; ############### The first pass: for my $row ( @data ) { $hash_old{$row->[0]} = $row->[1]; $main{$row->[0]} = $row->[1]; } my %hash_new; ############### All the remaining steps: do { for my $row (@data) { if ( $hash_old{$row->[1]} ) { $hash_new{$row->[0]} = $row->[1]; $main{ $row->[0] } = $main{ $row->[1] } || $row->[1]; } } %hash_old = (); $hash_old{$_} = $hash_new{$_} for keys %hash_new; %hash_new = (); } while ( scalar keys %hash_old ); ############### Filling in the main number ############### for the main number themselves: for my $row ( @data ) { $main{$row->[1]} = $row->[1] unless exists $main{$row->[1]} or $row->[1] eq ""; $main{$row->[0]} = $row->[0] unless length($main{$row->[0]}); } # Sample chains: # 123-234-345-456-567-678-789 # 117-228-339 # 131 # 213-324-435 # 372 dd \%main;

The output now:

{ 117 => 117, 123 => 123, 131 => 131, 213 => 213, 228 => 117, 234 => 123, 324 => 213, 339 => 117, 345 => 123, 372 => 372, 435 => 213, 456 => 123, 567 => 123, 678 => 123, 789 => 123, }

In reply to Re^2: Looking for the first item in the chain by vagabonding electron
in thread Looking for the first item in the chain by vagabonding electron

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.