reverse chomp()

E

EZP

Hi all,
when i have a value like "max ", the chomp command will cut off
the spaces to "max".

How can i make " max" into "max"? (i need to cut the spaces from
the begining of the word.

thanks,
EZP
 
J

John Bokma

EZP said:
Hi all,
when i have a value like "max ", the chomp command will cut off
the spaces to "max".

You're asking your question in the wrong group, since Perl's chomp command
does something entirely different.

perldoc -f chomp
How can i make " max" into "max"? (i need to cut the spaces from
the begining of the word.

perldoc perlrequick
Search and replace, if you want to search for leading space, and want to
replace it with nothing.

Other option is to match the first non white space looking from the start,
up until the end of the line.

Another option, if the leading space is always the same amount, you're
interested in the substring of the line that starts at position x.

perldoc -f substr
 
U

Uri Guttman

E> Hi all,
E> when i have a value like "max ", the chomp command will cut off
E> the spaces to "max".

where did you get that idea? chomp removes a trailine line ending (or
what is in $/), not spaces.

E> How can i make " max" into "max"? (i need to cut the spaces from
E> the begining of the word.

someone else answered that. but you should read perldoc -f chomp and
learn what it really does.

uri
 
A

alwaysonnet

Question can be best quoted as " Stripping leading and trailing white
spaces "

HTH,
Raj
 
U

Uri Guttman

a> Question can be best quoted as " Stripping leading and trailing white
a> spaces "

i think i can figure that out.

uri
 
D

David H. Adler

E> Hi all,
E> when i have a value like "max ", the chomp command will cut off
E> the spaces to "max".

where did you get that idea? chomp removes a trailine line ending (or
what is in $/), not spaces.

Well, it would do what the OP suggests if $/ were to contain the right
number of spaces... :)

dha
 
I

ikeon

EZP said:
Hi all,
when i have a value like "max ", the chomp command will cut off
the spaces to "max".

How can i make " max" into "max"? (i need to cut the spaces from
the begining of the word.

thanks,
EZP

You can try regex:

$test = " max";
$test =~ s/^\s*(\w*)/$1/g;
print "$test\n";
 
T

Tad McClellan

EZP said:
How can i make " max" into "max"?


By reading the Perl FAQ.

perldoc -q space

How do I strip blank space from the beginning/end of a string?
 
D

Dr.Ruud

ikeon schreef:
You can try regex:

$test = " max";
$test =~ s/^\s*(\w*)/$1/g;
print "$test\n";

Why would you do anything if there was no whitespace?
Why limit to \w, why not \S?
Why the g-modifier?

Consider: s/^[[:blank:]]+//
 
J

Jürgen Exner

EZP wrote:

Subject: "reverse chomp()"
You just add the newline back. If you are asking about a whole array then I
think you will have to loop through the array using 'for' or 'map' or
similar. At least I am not aware of an append that operates on a whole
array.
when i have a value like "max ", the chomp command will cut off
the spaces to "max".

Well, no, chomp() doesn't do that.
How can i make " max" into "max"? (i need to cut the spaces from
the begining of the word.

Your Question is Asked Frequently:
"How do I strip blank space from the beginning/end of a string?"

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,051
Members
47,656
Latest member
rickwatson

Latest Threads

Top