Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Looking for the first item in the chain

by graff (Chancellor)
on Aug 09, 2014 at 23:20 UTC ( [id://1096876]=note: print w/replies, xml ) Need Help??


in reply to Looking for the first item in the chain

I think you only need one hash, and you just have to loop over it as often as necessary, to eliminate intermediate links in the chains:
use strict; use warnings; my %parent_of; <DATA>; # skip header while (<DATA>) { chomp; my ( $child, $parent ) = split( /;/ ); $parent ||= $child; $parent_of{$child} = $parent; $parent_of{$parent} = $parent unless ( exists( $parent_of{$parent} + )); } my $changes; do { $changes = 0; for my $child ( keys %parent_of ) { my $parent = $parent_of{$child}; while ( $parent_of{$parent} != $parent ) { my $next_parent = $parent_of{$parent}; $parent_of{$child} = $next_parent; $parent = $next_parent; $changes++; } } } while ( $changes ); for my $child ( sort keys %parent_of ) { print " $child => $parent_of{$child}\n"; } __DATA__ NUM;NUMPRED 567;456 456;345 345;234 234;123 339;228 228;117 131; 435;324 324;213 372; 789;678 678;567
(I commend you for using Text::CSV_XS, and you can certainly stick with that, but I assumed the input would be simple enough to do without it.) The DATA above includes some extra samples, to create a longer chain.

The point of the do {...} while ( $changes ) loop is simply to keep iterating over the "parent_of" hash, changing values for child keys until all the values represent "primary nodes" in all the chains.

Apart from that, I like the other suggestion given above: if possible, it would make more sense to create this form output as part of the same process that produces the sample input you've shown us here.

Replies are listed 'Best First'.
Re^2: Looking for the first item in the chain
by vagabonding electron (Curate) on Aug 10, 2014 at 12:45 UTC

    Thank you very much graff ! Your example is very informative and self-explanatory. As for the data itself, it exists in the database in the form I showed, I am free to build the chains of numbers, but they does not exist in the raw form.

    Update: I benchmarked your solution with this of mine ( Re^2: Looking for the first item in the chain ) - your code is 48% faster!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-28 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found