Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Problems? Is your data what you think it is?
 
PerlMonks  

generating rows(matrix) based on file content

by zing (Beadle)
on Apr 23, 2013 at 13:08 UTC ( #1030134=perlquestion: print w/ replies, xml ) Need Help??
zing has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have this file(space separated)

___INPUT____ run1 2 4 run2 1 6 run3 1 run4 1 3 run5 2 5

The input file basically tells us the positions which are in on state for a device with 6 switches. Thus for example first row "2 4" means only 2nd and 4th switch are on.

What I want to generate is this. (I also need to keep the row descriptors : run1, run2 etc)

___OUTPUT___ run1 0 1 0 1 0 0 run2 1 0 0 0 0 1 run3 1 0 0 0 0 0 run4 1 0 1 0 0 0 run5 0 1 0 0 1 0

Comment on generating rows(matrix) based on file content
Select or Download Code
Re: generating rows(matrix) based on file content
by MidLifeXis (Prior) on Apr 23, 2013 at 13:12 UTC

    What have you tried?

    --MidLifeXis

Re: generating rows(matrix) based on file content
by blue_cowdawg (Prior) on Apr 23, 2013 at 13:16 UTC
        What I want to generate is this. (I also need to keep the row descriptors : run1, run2 etc)

    There are many ways to skin this cat and the cat isn't happy about it.

    #!/usr/bin/perl -w use strict; while (my $line=<DATA>){ chomp $line; my($run,@sw)=split(/[\s]+/,$line); my @switches=(); $switches[$_] = 0 foreach (0..5); # initialize switch array $switches[$_ -1]=1 foreach (@sw); printf "%s %s\n",$run,join(" ",@switches); } exit(0); __END__ run1 2 4 run2 1 6 run3 1 run4 1 3 run5 2 5


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: generating rows(matrix) based on file content
by choroba (Abbot) on Apr 23, 2013 at 13:17 UTC
    Just go through the input file and assign ones to a new array to given indexes:
    while (my $line = <>) { chomp $line; my (@columns) = split ' ', $line; my @switches = (0) x 6; unshift @switches, shift @columns; # runX kept. $switches[$_] = 1 for @columns; print join(' ', @switches), "\n"; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: generating rows(matrix) based on file content
by hdb (Parson) on Apr 23, 2013 at 13:33 UTC

    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

      ...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
Re: generating rows(matrix) based on file content
by LanX (Abbot) on Apr 23, 2013 at 21:04 UTC
    Golfing fun with slices! =)

    while (<DATA>) { my @array = (0) x 7; ($array[0], my @pos) = split /\s+/; @array[@pos] = (1) x @pos; print "@array\n"; } __DATA__ run1 2 4 run2 1 6 run3 1 run4 1 3 run5 2 5

    OUTPUT

    run1 0 1 0 1 0 0 run2 1 0 0 0 0 1 run3 1 0 0 0 0 0 run4 1 0 1 0 0 0 run5 0 1 0 0 1 0

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030134]
Approved by dfaure
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2014-04-23 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (541 votes), past polls