#!/usr/bin/perl ## ## builds %struct_hash with following inside: ## keys are identifiers .. ## `$struct_hash{$_}->{'str'} is utf8 string descr ## `$struct_hash{$_}->{'parent'}`is full parent identifier ## `$struct_hash{$_}->{'plen'} is parent's length ## in 'parent' is sequence n(n+1)=L so we're using n=sqrt(4L+1)-1 ## for determining last increment ## use strict; use warnings; my $csv_file=$ARGV[0]; my %struct_hash; ##### functions sub read_csv { my @csv; open my $fh, "$csv_file" or die "bad file: \"$csv_file\" - can't open\n"; push(@csv, "$_") while(<$fh>); close $fh; return @csv; }; sub build_struct_hash { my %struct_hash; foreach(read_csv()) { my($pos_xy, $pos_str) = split(/;/, "$_"); $pos_xy =~ s/\D//g; $pos_str =~ s/\r//g; chomp($pos_str); my $len = length($pos_xy); $struct_hash{$pos_xy} = {}; $struct_hash{$pos_xy}->{'str'} = $pos_str; $struct_hash{$pos_xy}->{'parent'} = substr($pos_xy, 0, ($len - (sqrt(4*$len+1)-1))); $struct_hash{$pos_xy}->{'plen'} = length($struct_hash{$pos_xy}->{'parent'}); }; return %struct_hash; }; ########### main body: %struct_hash = build_struct_hash(); ########## user body: ## example # for work with all keys we need to do it like this: foreach(sort {$a <=> $b} keys(%struct_hash)) { # print "$_ ", length, " $struct_hash{$_}->{'str'}"; # print " $struct_hash{$_}->{'parent'} $struct_hash{$_}->{'plen'} $struct_hash{$struct_hash{$_}->{'parent'}}->{'str'}\n"; print "$struct_hash{$_}->{'str'}\n" if(!$struct_hash{$_}->{'parent'}); }; exit;