Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Replacing Whitespace with Underscore

by CPTQuesoXI (Novice)
on Apr 09, 2018 at 16:04 UTC ( #1212594=perlquestion: print w/replies, xml ) Need Help??

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

I am performing a phylogenetic tree from sequence alignments with a software that requires no whitespaces and no brackets/parentheses in the text file. I try to substitute my whitespaces with underscores and my brackets with whitespaces so that the file can be read without problem in the alignment software. My code runs without error warnings, but does not give any output whatsoever.

foreach my $line (<>) { chomp ($line); if ($line =~ m/^>/) { #if line matches ">" at beginnin +g if ($line =~ /\s/g) { #if line matches whitespa +ce globally $line =~ s/\]/\s/; $line =~ s/\[/\s/; $line =~ s/\s /_/g; #substitut +e whitespace with underscore. } } }

Replies are listed 'Best First'.
Re: Replacing Whitespace with Underscore
by pryrt (Monsignor) on Apr 09, 2018 at 16:19 UTC

    If that is your complete code, you are missing any output statements. You have edited the variable $line, which edits the string in memory... but then you do nothing with it. Given that you've chomped the line, I recommend something like print "$line\n"; at the end of your foreach loop

      I'm angry with myself for forgetting that. That ended up given me exactly what I needed. Thank you!

Re: Replacing Whitespace with Underscore
by jimpudar (Pilgrim) on Apr 09, 2018 at 16:24 UTC

    You should turn on 'use strict' and 'use warnings', this will help lead you to a better solution.

    It seems your main problem might be that you are confusing the space character ' ' with the whitespace character class.

    In a regex, '\s' matches not only the space character, but ANY whitespace character. See Whitespace.

    Your substitutions like 's/\[/\s/' will fail because '\s' is only recognized as whitespace in a regex:

    $ perl -wE 'my $str = "JA_PH"; $str =~ s/_/\s/; say $str' Unrecognized escape \s passed through at -e line 1. JAsPH $ perl -wE 'my $str = "JA_PH"; $str =~ s/_/ /; say $str' JA PH

    Also, your regex for substituting whitespace with underscore will only match a whitespace character followed by a space character. I don't think this is quite what you meant.

    $ perl -wE 'my $str = "Some text with whitespace"; $str =~ s/\s /_/g; + say $str' Some text with_whitespace $ perl -wE 'my $str = "Some text with whitespace"; $str =~ s/\s/_/g; +say $str' Some_text_with__whitespace $ perl -wE 'my $str = "Some text with whitespace"; $str =~ s/\s+/_/g; + say $str' Some_text_with_whitespace

    Best,

    Jim

      Thanks! It worked perfectly!

Re: Replacing Whitespace with Underscore
by Anonymous Monk on Apr 09, 2018 at 16:40 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2020-10-22 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (225 votes). Check out past polls.

    Notices?