954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

skip leading space problem

Hi, I have encounter a strange result couldn't figured out.

# Str with format "Mon MM HH", try to skip leading space in DD.
my $str = "Jan 8 11"; # Jan, 8th 11 o'clock
print substr($Str, 0,3) . substr($Str, 4,2)=~s/^\s+// . substr($Str, 7,2) . "\n";

The result is "Jan11", I expect "Jan811". what is going on?

If I separate it to one more step:
my $no_space = substr($Str, 4,2);
$no_space =~ s/^\s+//;

then use $no_space replace BOLD part, everything is fine. Could anyone fix the
problem. Thanks in advance.

j111c222
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

You want something like this?

my @a = split(/\s/, $str);
say "$a[0]$a[1]$a[2]";
replic
Light Poster
47 posts since Nov 2008
Reputation Points: 27
Solved Threads: 6
 

Hi, I have encounter a strange result couldn't figured out.

# Str with format "Mon MM HH", try to skip leading space in DD.
my $str = "Jan  8 11";   # Jan, 8th 11 o'clock
print substr($Str, 0,3) . substr($Str, 4,2)=~s/^\s+//. substr($Str, 7,2) . "\n";

The result is "Jan11", I expect "Jan811". what is going on?

substr takes an expression (here $str), an offset (index of starting character) and length. You need to keep in mind that offset is zero based.

In your example, you take the first 3 characters ("Jan"), then concatenate that with the characters at index 4 and 5 (" "), which you strip of the spaces (""), then the characters at index 7,8 ("11").

Perl is an excellent language in that you don't need to "translate" what you want to do with how you need to write it. If what you want is to "strip all whitespace from $str" then simply write

$str =~ s/\s+/g


(the g modifier means the action will be repeated as long as necessary. If your intention is different, a different solution can be used.

erezschatz
Newbie Poster
18 posts since Jan 2011
Reputation Points: 26
Solved Threads: 4
 

substr takes an expression (here $str), an offset (index of starting character) and length. You need to keep in mind that offset is zero based.

In your example, you take the first 3 characters ("Jan"), then concatenate that with the characters at index 4 and 5 (" "), which you strip of the spaces (""), then the characters at index 7,8 ("11").

Perl is an excellent language in that you don't need to "translate" what you want to do with how you need to write it. If what you want is to "strip all whitespace from $str" then simply write

$str =~ s/\s+/g
(the g modifier means the action will be repeated as long as necessary. If your intention is different, a different solution can be used.

Excellent alternative @erezschatz, and good explanation of the original problem, except for when you saythe characters at index 4 and 5 (" ") because the characters at
index 4 and 5 look to me like "8 " as the following demonstrates:

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

#The following is the OP's example (typos $Str corrected to $str)
my $str = "Jan 8 11"; # Jan, 8th 11 o'clock
#print substr($str, 0,3) . substr($str, 4,2)=~s/^\s+// . substr($str, 7,2) . "\n";

#OK. What is the value of substr($str, 4,2) without the substitution?
my $middle_substring = substr($str, 4,2);
say "*****$middle_substring*****"; #Prints *****8 *****! Why?

#Let's print what the middle part of the above gives you.
my $middle_substring_stripped = substr($str, 4,2)=~s/^\s+//;
say "*****$middle_substring_stripped*****"; #$middle_substring_stripped is empty.
#What happened to the 8?
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

You're right. For some reason, I saw a double space there.

erezschatz
Newbie Poster
18 posts since Jan 2011
Reputation Points: 26
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You