perl hash -- best way to re index to compact values -


There is a multi-dimensional hash in Perl with my columns and rows.

What is the best way to compact / reindex it:

  • Remove empty columns
  • Remove empty rows
  • Reindex
  • Individual items / cells with values ​​just do not need to change columns in rows.

Thank you ...

Here is a sample hash that is similar to the structure that I have:

  foreach $ i (1..5) {$ Column = int (rand (10)) +1; Aggression $ j (1..10) {$ row = int (rand (10)) +1; $ Value = int (RAND (1000)) +1; $ Hash {$ column} {$ row} = $ value if! $ Hash {$ column} {$ row}; }}  

+ ------------------------------------ -------------- +

I changed it to an array of an array.

I was able to compress the columns but I am unable to remove empty columns, I know that there is some piece of it but can not detect the correct syntax.

  # AOA - # $ VAR1 = first [unfollowed, [4, 0, 61, 0, 22], [underneath, 0, 0, 0, 0, 0], [ Egg, 0, 12, 50, 0, 66], [undershot, 70, 42, 22, 0, 0]]; Forex Currency $ (0 .. AoA-1) {### Compact Column @ {$ AoA [$ column]} = grep {($ _)} @ $$ AoA [$ column]}; ### Delete empty columns ### What is the correct syntax for this ??? # If (! (Scalar @ {$ AoA [$ column]}) # # Bunt (@aoa, $ column, 1); #} - # $ VAR1 = [[], [4, 61, 22], [], [12, 50, 66], [70, 42, 22]] #AOA -; Take a look at  

, it provides many features that you can do Allows

Example table

  my $ t = new data: table ([["John", 20], ["Kate", 18], ["Mike" , 23]], ["name", "age"], 0); My $ t1 = new data :: table; For example, get a new table without the empty rows  
  $ t1 = $ t- & gt; Match_pattern ('join (' ', $ _) -> gt; = ~ //');  

Find and delete empty columns

  my $ columns_keep = {}; $ T- & gt; ColsMap (sub {$ columns_keep {$ _- & gt; [$ colIndex]} = "1" if $ _- & gt; [0];}); $ T1.addCols ($ t- & gt; Dell calls (key $ columns_keep));  

Comments