Here's how you would use the subroutine:sub combine_rows { my ( $label, @arrays ) = @_; my @joined; for my $i ( 0 .. $#{$arrays[0]} ) { # assumes that all arrays are + the same size push @joined, join( ';', $label, map { $arrays[$_][$i] } 0 .. +$#arrays ); } return \@joined; }
Now, it would be a little more complicated if your input array values can contain the semi-colon character as data. In that case, you need to escape or quote the field values in the subroutine. (That's the sort of thing Text::CSV would do for you if you used it properly, but it's not that complicated to do on your own.)@ports = qw( portname portID port802 ); @val1 = qw( ON ON OFF ); @val2 = qw( EX EX NEX ); my $joined = combine_rows( "ports", \@ports, \@val1, \@val2 ); print "$_\n" for ( @$joined );
The reason I use "map" in the subroutine is to allow it to join any number of parallel arrays (not just exactly 3, as in your "@ports, @val1, @val2" example). If you can't figure out what "map" is doing there, it's just a nested loop to get the $i'th value of each input array.
In reply to Re^3: Creating CSV file
by graff
in thread Creating CSV file
by reez
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |