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.

You want something like this?

my @a = split(/\s/, $str);
say "$a[0]$a[1]$a[2]";

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.

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 say

the 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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.