I have an array like this (it\'s just a little overview but it has 2000 and more lines like this):
@list = (
\"affaire,chose,question\",
\"ca
For each element in @list
, split it at ,
, and use each field as key of %te
, push others to the value of that key:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @list = (
"affaire,chose,question",
"cause,chose,matière",
);
my %te;
foreach my $str (@list) {
my @field = split /,/, $str;
foreach my $key (@field) {
my @other = grep { $_ ne $key } @field;
push @{$te{$key}}, @other;
}
}
print Dumper(\%te);
Ouput:
$ perl t.pl
$VAR1 = {
'question' => [
'affaire',
'chose'
],
'affaire' => [
'chose',
'question'
],
'matière' => [
'cause',
'chose'
],
'cause' => [
'chose',
'matière'
],
'chose' => [
'affaire',
'question',
'cause',
'matière'
]
};