There is an array of arrays:

dump(@test) ( ["hello1", "world1"], ["hello2", "world123"], ["hello3", "world3"], ["hello3", "world213"], ["hello3", "world0"], ["hello1", "world9"], ["hello20", "world8"], ["hello20", "world7"], ) 

How do I get all the entries from this first for hello1, then hello2, etc.? For example, in a file like

 [hello1] world1 world9 [hello2] world213 etc 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

4 answers 4

I think we need to convert it into a hash of arrays and then work with it:

 my(@test)=( ["hello1", "world1"], ["hello2", "world123"], ["hello3", "world3"], ["hello3", "world213"], ["hello3", "world0"], ["hello1", "world9"], ["hello20", "world8"], ["hello20", "world7"] ); my($k,$v,@v,%hash); foreach $v (@test) { @v=@{$v}; $hash{$v[0]}[@{$hash{$v[0]}}]=$v[1]; } foreach $k (sort keys(%hash)) { print "[$k]\n"; foreach $v (@{$hash{$k}}) { print "$v\n"; } } 

Only sort will need to feed its handler, which correctly sorts strings.

    I agree about converting to hash, but sorting through a function may not be so convenient. If there the order of the rows does not depend on the alphabet, or anything else ordered, then you can kill a lot of time for the implementation of the sorter. Why not use an array of keys as a supplement?

     my @list = ( ["hello1", "world1"], ["hello2", "world123"], ["hello3", "world3"], ["hello3", "world213"], ["hello3", "world0"], ["hello1", "world9"], ["hello20", "world8"], ["hello20", "world7"], ); my %h; my @order; foreach my $i (@list){ my $key = $i->[0]; unless($h{$key}){ $h{$key} = []; push @order, $key; } push $h{$key}, $i->[1]; } # show result while( my $k = shift @order ){ print "[$k]\n"; print "$_\n" foreach @{ $h{$k} }; } 
    • Damn, when a book on perl is brought to me, you can see that your code is much more beautiful than what I get :) Only push $h{$key}, $i->[1] does not work, push @{$h{$key}}, $i->[1] . And the array unfortunately will help you only due to the fact that the initial data is sorted, if in the initial data hello20 at the beginning, then the output will be at the beginning - Mike
    • Yes, about @ {$ h {$ key}} I’ve done it. And here about sorting I proceeded from the fact that it is important to adhere to the same order as in the input data. Well, if it is necessary alphabetically, sort will help :) - Infarch
     use strict; use warnings; my @test = ( ["hello1", "world1"], ["hello2", "world123"], ["hello3", "world3"], ["hello3", "world213"], ["hello3", "world0"], ["hello1", "world9"], ["hello20", "world8"], ["hello20", "world7"], ); my %h; map { push do { $h{ $_->[0] } //= [] }, $_->[1] } @test; map { print "[$_]\n" , join("\n", sort @{ $h{$_} }), "\n" } sort keys %h; __END__ Выдаст: [hello1] world1 world9 [hello2] world123 [hello20] world7 world8 [hello3] world0 world213 world3 

      To upload a file of a specified format, use the YAML module and at the output you will get what you need.