utpalmtbi has asked for the wisdom of the Perl Monks concerning the following question:

Monks!!!

I try to trim file content headers in the format '>bla|bla|bla' in one or two fields. For that I have used:

while (<>) { if (/^>/) { my @fields = split '\|', $_; if (@fields <= 2) { print "$fields[0]\n"} else { print join '|', "@fields[0,1]\n" } next; } print; }

When it prints two fields (second print) it prints like this >bla bla (that is, a space in between), but I need to print them with a pipe in between and with no space (that is >bla|bla).

Without writting another regex to replace space with pipe, how to do that in current script?

Thanks

Replies are listed 'Best First'.
Re: Join without space in this perl script
by hippo (Archbishop) on Nov 19, 2016 at 10:14 UTC
    print join '|', "@fields[0,1]\n"

    Running join on a scalar string like that will have no effect. You should join a list instead.

    #!/usr/bin/perl use strict; use warnings; my @fields = qw/foo bar baz/; print join ('|', @fields[0,1]) . "\n";
      right.. Thank you !!