I'm converting a one dimensional array into a multidimensional array. See the below codefor what I am doing. I'm still learning perl and so want to have an improvement on my code.
My Question: Can I do the conversion in one line?
I think Perl is able to do so. When writing a subroutine it's possible to assign:
my ($one, $two, $three) = @_;
My code:
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my @oneDim = (
'COL', 'EUR', 'Fruit', 'apple',
'HZP', 'USD', 'Fruit', 'banana',
'HKG', 'USD', 'Food', 'burger',
'IML', 'USD', 'IT', 'keyboard');
# (@fourDim[0],@fourDim[1],@fourDim[2],@fourDim[3]) = @oneDim;
sub main() {
my @fourDim = ([],[],[],[]);
for (my $i = 0; 4 * $i + 3 < scalar(@oneDim); ++$i)
{
push @{$fourDim[0]}, $oneDim[$i * 4 + 0];
push @{$fourDim[1]}, $oneDim[$i * 4 + 1];
push @{$fourDim[2]}, $oneDim[$i * 4 + 2];
push @{$fourDim[3]}, $oneDim[$i * 4 + 3];
}
print Dumper @fourDim;
}
main();
And this is the result:
$VAR1 = [
'COL',
'HZP',
'HKG',
'IML'
];
$VAR2 = [
'EUR',
'USD',
'USD',
'USD'
];
$VAR3 = [
'Fruit',
'Fruit',
'Food',
'IT'
];
$VAR4 = [
'apple',
'banana',
'burger',
'keyboard'
];
I found the reverse conversion here: https://stackoverflow.com/a/8611430/2764334
But I could not find an elegant way to convert a one dimensional array into a multidimensional. In my case I know that there is always the same order (code, currency, type, product)