Using Raku (formerly known as Perl_6)
~$ raku -ne 'my @a = .split(","); \
@a[2..*] = do for @a[2..*] { $_ ~~ 0.Int ?? 0 !! 1 }; \
@a.join(",").put ;' file
OR:
~$ raku -ne 'my @a = .split(","); \
@a[2..*] .= map: { $_ ~~ 0.Int ?? 0 !! 1 }; \
@a.join(",").put ;' file
OR:
~$ raku -ne 'my @a = .split(","); \
@a[2..*] .= map: { +( $_ !~~ 0.Int ) }; \
@a.join(",").put;' file
Raku is a programming language in the Perl-family of programming languages. It features built-in, high-level support for Concurrency, Asynchrony, and Parallelism (CAP).
Above, the first two code examples bear remarkable similarity to the excellent Perl answers posted by @cas. Of note, Raku has an asymmetric ~~
"smartmatching" operator, which is syntactic sugar for Raku's .ACCEPTS()
method (i.e. "Does the RHS accept the LHS?"). Many "Type"-related issues can be resolved using Raku's ~~
"smartmatching" operator.
Raku also has a new format for the built-in ternary operator: (Test) ??
True !!
False. Some people find this ternary operator easier to read. Alternatively, the third example uses the fact that ~~
sets the return variable after comparison, allowing the resultant True
/False
values to be coerced via +(…)
or (…).Int
to 0
/1
.
[In the code examples above, the RHS .Int
is actually superfluous, and smartmatching works quite well without any additional coercions. However you can perform LHS/RHS coercion ( via .Str
, .Int
, .Bool
, etc.) if you have the need for a specific comparison not addressed by any of the defaults].
Sample Input:
Iss1,1,0,0,Hsapiens-I34,0,0,0,Mmusculus-H01,0,0
Iss1,11,0,Scerevisiae-U09,Hsapiens-I05,0,0,0,0,0,0
Iss1,21,0,0,Hsapiens-I05,0,0,0,Hsapiens-I31,0,0
Iss1,31,0,0,Mmusculus-H13,0,0,0,0,0,Hsapiens-I31
Iss1,41,0,Scerevisiae-U09,0,0,0,0,0,0,Hsapiens-I21
Iss1,51,0,0,0,0,0,0,Scerevisiae-U25,0,Hsapiens-I21
Iss1,61,0,0,Hsapiens-I34,0,0,0,Mmusculus-H13,0,0
Sample Output:
Iss1,1,0,0,1,0,0,0,1,0,0
Iss1,11,0,1,1,0,0,0,0,0,0
Iss1,21,0,0,1,0,0,0,1,0,0
Iss1,31,0,0,1,0,0,0,0,0,1
Iss1,41,0,1,0,0,0,0,0,0,1
Iss1,51,0,0,0,0,0,0,1,0,1
Iss1,61,0,0,1,0,0,0,1,0,0
Note: the above code will convert blank values, 00
, 0x0
, -0
, and 0.0
all to 0
zeroes. In particular for blank (empty) values, you should verify that all columns are filled with the code raku -ne '.split(",", :skip-empty).elems.say;'
, with/without the :skip-empty
parameter to detect differences. For Perl fans, see the Raku Docs section '0' is True.
Take a look at Perl answers if you want a different treatment for blank values, 00
, 0x0
, -0
, and 0.0
. The Perl answer by @StéphaneChazelas includes an explanation of how these values (and blanks) are treated. In my hands, the Perl answer by @cas changes blank values, 00
, 0x0
, -0
, and 0.0
all to 1
(different treatment for blank values than @StéphaneChazelas Perl answer). So you have your choice (two languages, three treatments)!
https://docs.raku.org/language/operators#infix_??_!!
https://docs.raku.org/routine/ACCEPTS
https://docs.raku.org/routine/~~
https://raku.org