If I declared a variable $myString
with the value \'3 \'
(notice the white space).
Is there any function to remove the white space for the return v
I suggest that you make use of the Text::Trim module, which provides ltrim
, rtrim
, and trim
, all of which will trim the parameters passed, or $_
if you give no parameters. It's not a core module so it may need installing
Try this:
# Delete leading/trailing whitespace.
$string =~ s/^\s+|\s+$//g;
Remove all spaces in a string:
$string =~ s/ //g;
Just looking over your program, I found 3 spots that could be improved or fixed.
I apologize if my code doesn't format well. :-(
In your function parse_block(...), there are 3 items that need attention.
@attribs = $attribs =~ /\s*(\w+\s+=\s+\w+\s+|\w+\s+=\s+".*?"|\w+\s+=\s+<.*?>)\s*/g;
To eliminate the white space after vid => '6 ', just don't include the \s+ at the end of your first sub-regex.
Write it as:
@attribs = $attribs =~ /\s*(\w+\s+=\s+\w+|\w+\s+=\s+".*?"|\w+\s+=\s+<.*?>)\s*/g;
$value = [ parse_type_value_specifier( $start_tail ) ];
You want this instead:
$value = [ parse_type_value_specifier( $value ) ];
(Note that the parameter to the function should be $value and not $start_tail.) You probably didn't notice this.
In the loop for @attributes, the 'else' in the if/else condition excutes when the 'value' has a plain value, (no "" or <...> items in 'value').
Update: Changed parameter in
parse_type_value_specifier(...)to $value. It was (incorrectly) stated as $attrib.
Remove spaces from variable $test (eq rtrim(ltrim(@sStr))
from Transact SQL:
$test =~s/^\s*(\S*)\s*$/$1/;
Another potential alternative solution is Text::Trim from CPAN, which will "remove leading and/or trailing whitespace from strings". It has a trim
function which may suit your needs.