Find all lines in the file that are the same and print the value after in perl

Good day guys.

I am trying to print from an excel file to an ini file, I have excel and ini sorting, but my problem is getting the values ​​without duplication.

The excel file will contain something like this

House | 133_Anderson_str Shop | 12_Smith_Str Wheelshop | 832_Smith_Str House | 21_smith_Str Shop | 1191_dandelion_rd 

If I do foreach and print it in an ini file, I get this result

  [House] adress=133_Anderson_str [Shop] adress=12_Smith_Str [Wheelshop] address=832_Smith_Str [House] adress=21_smith_Str [Shop] address=1191_dandelion_rd 

This is not possible, because if I run gui, which will look for all the addresses under the house, it will only refer to the first and then ends on a new line. I want to print each duplicate value once, and then the next value after printing it as follows

  [House] adress=133_Anderson_str, 21_smith_Str [Shop] adress=12_Smith_Str, 1191_dandelion_rd [Wheelshop] address=832_Smith_Str 

I tried the has table, but I cannot print in the correct order and make sure that each value is printed on the correct key and does not duplicate the key.

I use Config :: Inifiles to print and read ini and SpreadSheet :: Read to get data from a spreadsheet.

Hope someone can show me the light!

Thanks to the mill.

+4
source share
1 answer

It should be an @input array with 5 lines / elements,

 House | 133_Anderson_str Shop | 12_Smith_Str Wheelshop | 832_Smith_Str House | 21_smith_Str Shop | 1191_dandelion_rd 

First, %hash used to group @input elements into a hash of an array structure,

 my %hash; for my $line (@input) { # $k = "House"; $v ="133_Anderson_str", etc. my ($k, $v) = split / [\s\|]+ /x, $line; my $arr = $hash{$k} ||= []; push @$arr, $v; } 

Now that the grouping is complete, continue with the output,

 for my $k (sort keys %hash) { my $arr = $hash{$k}; my $vals = join ", ", @$arr; print "[$k]\n", "address=$vals\n\n"; } 
+4
source

Source: https://habr.com/ru/post/1483088/


All Articles