我在perl中有列和行的多維哈希。perl hash - 重新索引到緊湊值的最佳方式
什麼是壓縮/它重新索引以最好的方式,即用價值,是少的下一行指數
- 刪除空列
- 刪除空行
- 重新編制單個項目/細胞比當前行索引
- 具有值的單個項目/單元格不需要僅更改行的列。
謝謝你......
下面是一個簡單的哈希在結構上與我有什麼相似:
foreach $i (1..5) {
$column = int(rand(10)) +1;
foreach $j (1..10) {
$row = int(rand(10)) +1;
$value = int(rand(1000)) +1;
$hash{$column}{$row} = $value if !$hash{$column}{$row};
}
}
+ ------------ -------------------------------------- +
我將它轉換爲一個數組陣列。
我能夠壓縮列,但我無法刪除空列。我知道它與切片有關,但無法弄清楚正確的語法。
#AoA - Before#
$VAR1 = [
undef,
[
undef,
4,
0,
61,
0,
22
],
[
undef,
0,
0,
0,
0,
0
],
[
undef,
0,
12,
50,
0,
66
],
[
undef,
70,
42,
22,
0,
0
]
];
foreach $column ([email protected] -1) {
### Compact column
@{$AoA[$column]} = grep { ($_) } @{$AoA[$column]};
### Remove empty column
### What is the correct syntax for this???
# if(!(scalar @{$AoA[$column]})) {
# splice(@AoA, $column, 1);
# }
}
#AoA - After#
$VAR1 = [
[],
[
4,
61,
22
],
[],
[
12,
50,
66
],
[
70,
42,
22
]
];
可以前後重新索引你提到後顯示的例子嗎?更好的是,運行你顯示的代碼,顯示數據結構的轉儲(例如'print Data :: Dumper :: Dumper(\%hash)'的輸出),然後在你想要的所有更改之後顯示它的外觀 – ysth 2014-10-28 00:40:14