All pastes #1992045 Raw Edit

dpecka

public text v1 · immutable
#1992045 ·published 2010-11-15 10:47 UTC
rendered paste body
#!/usr/bin/perluse strict;use warnings;my $csv_file="@ARGV";my %struct_hash;##### functionssub 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;	};	return %struct_hash;};sub get_parent_id {	my $len_tmp = length($_[0]);	my $plen = 2;	my $incr = 2;	my $len;	$len = 2 if length($_[0]) == 2;	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)) {	## 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";};exit;