Come posso attivare un tavolo in una matrice?
-
22-08-2019 - |
Domanda
Se ho ottenuto un tavolo in un file di testo, ad esempio come
- A B 1
- A C 2
- A D 1
- B A 3
- C D 2
- A E 1
- E D 2
- C B 2
- . . .
- . . .
- . . .
E ho avuto un altro elenco simbolo in un altro file di testo. Voglio trasformare questa tabella in una struttura dati Perl come:
- _ A D E. . .
- A 0 1 1. . .
- D 1 0 2. . .
- E 1 2 0. . .
- . . . . . . .
Ma ho solo bisogno di un po simbolo selezionato, ad esempio A, D ed E sono selezionati nel testo simbolo, ma B e C non sono.
Soluzione
Utilizzare una matrice per il primo e un hash 2-dimensionale per il secondo. Il primo dovrebbe essere grosso modo così:
$list[0] # row 1 - the value is "A B 1"
E l'hash come:
$hash{A}{A} # the intersection of A and A - the value is 0
Per capire come implementare un problema è circa il 75% della battaglia mentale per me. Non ho intenzione di andare in specifiche su come stampare l'hash o la matrice, perché è facile e io non sono anche del tutto chiaro su come si desidera stampare o quanto da stampare. Ma la conversione l'array per l'hash dovrebbe apparire un po 'come questo:
foreach (@list) {
my ($letter1, $letter2, $value) = split(/ /);
$hash{$letter1}{$letter2} = $value;
}
Almeno, penso che è quello che stai cercando. Se davvero qualsiasi momento potrà usare un'espressione regolare, ma questo è probabilmente eccessivo per solo l'estrazione di 3 valori di una stringa.
EDIT: Naturalmente, si potrebbe rinunciare al @list
e solo assemblare il hash direttamente dal file. Ma questo è il vostro lavoro per capire, non la mia.
Altri suggerimenti
si può provare questo con awk:
awk -f matrix.awk yourfile.txt> newfile.matrix.txt
dove matrix.awk è:
BEGIN {
OFS="\t"
}
{
row[$1,$2]=$3
if (!($2 in f2)) { header=(header)?header OFS $2:$2;f2[$2]}
if (col1[c]!=$1)
col1[++c]=$1
}
END {
printf("%*s%s\n", length(col1[1])+2, " ",header)
ncol=split(header,colA,OFS)
for(i=1;i<=c;i++) {
printf("%s", col1[i])
for(j=1;j<=ncol;j++)
printf("%s%s%c", OFS, row[col1[i],colA[j]], (j==ncol)?ORS:"")
}
}
Un altro modo per farlo sarebbe quello di fare un array bidimensionale -
my @fArray = ();
## Set the 0,0th element to "_"
push @{$fArray[0]}, '_';
## Assuming that the first line is the range of characters to skip, e.g. BC
chomp(my $skipExpr = <>);
while(<>) {
my ($xVar, $yVar, $val) = split;
## Skip this line if expression matches
next if (/$skipExpr/);
## Check if these elements have already been added in your array
checkExists($xVar);
checkExists($yVar);
## Find their position
for my $i (1..$#fArray) {
$xPos = $i if ($fArray[0][$i] eq $xVar);
$yPos = $i if ($fArray[0][$i] eq $yVar);
}
## Set the value
$fArray[$xPos][$yPos] = $fArray[$yPos][$xPos] = $val;
}
## Print array
for my $i (0..$#fArray) {
for my $j (0..$#{$fArray[$i]}) {
print "$fArray[$i][$j]", " ";
}
print "\n";
}
sub checkExists {
## Checks if the corresponding array element exists,
## else creates and initialises it.
my $nElem = shift;
my $found;
$found = ($_ eq $nElem ? 1 : 0) for ( @{fArray[0]} );
if( $found == 0 ) {
## Create its corresponding column
push @{fArray[0]}, $nElem;
## and row entry.
push @fArray, [$nElem];
## Get its array index
my $newIndex = $#fArray;
## Initialise its corresponding column and rows with '_'
## this is done to enable easy output when printing the array
for my $i (1..$#fArray) {
$fArray[$newIndex][$i] = $fArray[$i][$newIndex] = '_';
}
## Set the intersection cell value to 0
$fArray[$newIndex][$newIndex] = 0;
}
}
Non sono troppo orgoglioso per quanto riguarda il modo in cui ho gestito i riferimenti ma recano con un principiante qui (si prega di lasciare i vostri suggerimenti / modifiche nei commenti). Il metodo di hash sopra citato da Chris suona molto più facile (per non parlare molto meno di battitura).
CPAN ha molti potenzialmente utile suff . Io uso Tabella dati :: per vari scopi. dati :: pivot sembra promettente, ma non ho mai usato.