in reply to substitute space to 0

#!/usr/bin/perl # https://perlmonks.org/?node_id=1220510 use strict; use warnings; while( <DATA> ) { s/(^| \K)(?!\d)/0/g; print; } __DATA__ 1 2 2 5 4 4 4 4 4 3 4 4 1 1 5 6 4

Replies are listed 'Best First'.
Re^2: substitute space to 0
by yueli711 (Sexton) on Aug 17, 2018 at 18:29 UTC

    Hello, tybalt89, Thank you so much for your great help.

    The data file is

    1 2 2 5 4 4 4 4 4 3 4 4 1 1 5 6 4

    It comes out

    1 2 2 5 4 4 4 4 4 3 0 4 4 1 0 1 5 6 4 #!/usr/bin/perl # https://perlmonks.org/?node_id=1220510 use strict; use warnings; open(DATA,"DATA")||die"cannot open the file: $!\n"; while( <DATA> ) { s/(^| \K)(?!\d)/0/g; print; }

      You have too old a perl, try

      #!/usr/bin/perl # https://perlmonks.org/?node_id=1220510 use strict; use warnings; while( <DATA> ) { s/(^| )(?!\d)/$&0/g; print; } __DATA__ 1 2 2 5 4 4 4 4 4 3 4 4 1 1 5 6 4
A reply falls below the community's threshold of quality. You may see it by logging in.