in reply to generating rows(matrix) based on file content

Here a version if the number of switches is not known upfront.

use strict; use warnings; my $max = 0; my $data; while(<DATA>){ chomp; my @array = split /\s/; my @line = ( shift @array ); $line[$_] = 1 for @array; $max = @line > $max ? @line : $max; push @$data, \@line; } for my $row (@$data) { print $row->[$_]//0 ," " for (0..$max-1); print "\n"; } __DATA__ run1 2 4 run2 1 6 run3 1 run4 1 3 run5 2 5

Replies are listed 'Best First'.
Re^2: generating rows(matrix) based on file content
by hdb (Monsignor) on Apr 23, 2013 at 20:41 UTC

    ...and one version based on regex if the number of switches is known:

    use strict; use warnings; my $n_switches = 6; while(<DATA>){ my $last = 0; s/\s(\d+)/my $d = $1-$last-1; $last = $1; " 0"x$d." 1" /ge; s/\n/" 0" x ($n_switches-$last)."\n"/e; print; } __DATA__ run1 2 4 run2 1 6 run3 1 run4 1 3 run5 2 5