#!/usr/bin/perl use strict; use warnings; my $csv_file="@ARGV"; 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); $struct_hash{$pos_xy} = {}; $struct_hash{$pos_xy}->{'str'} = $pos_str; }; my $len_last = 0; my $len_par = 0; foreach(sort {$a <=> $b} keys(%struct_hash)) { if(length != $len_last) { $struct_hash{$_}->{'parent'} = $len_par; $len_last = length; $struct_hash{$_}->{'len'} = length; } else { $struct_hash{$_}->{'parent'} = $len_par; $struct_hash{$_}->{'len'} = length; next; }; }; $len_par = $length; return %struct_hash; }; #sub get_parent_id { # my $len_tmp = length($_[0]); # my $plen = 2; # my $incr = 2; # my $len; # while($plen <= $len_tmp) { # $len = $plen; # $plen = $plen + (2*$incr); # $incr++; # }; # return substr($_[0], 0, $len); #}#; ########### 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 "$_"; print "\t\t$struct_hash{$_}->{'len'} $struct_hash{$_}->{'parent'} $struct_hash{$_}->{'str'}\n"; ## id in current loop # my $foo = "$_"; ## current loop's parent id # my $bar = get_parent_id("$_"); ## current loop id's key # my $baz = "$struct_hash{$foo}->{'str'}"; ## current loop id's parent key # my $heh = "$struct_hash{$bar}->{'str'}"; ## example of output format # print "$baz|$foo;$heh|$bar\n"; # print "$foo\n"; }; exit;