CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Add whitespace Beggining and End

 



duffnuts
New User

Mar 25, 2008, 4:42 PM

Post #1 of 5 (787 views)
Add whitespace Beggining and End Can't Post

Hi There,

I am trying to work out how to add a whitespace character to the begging and the end of a predefined string variable. The string variable can vary in length depending on the the user input.

I use the following to get user input and was wondering if I can add whitespace to the begging or the or the end at the same time or add the whitespace latter?
$Input = <STDIN>;

For example if the user enters "21" i actually want this to be stored as " 21 "

Thanks,

Duffnuts


KevinR
Veteran


Mar 25, 2008, 5:08 PM

Post #2 of 5 (786 views)
Re: [duffnuts] Add whitespace Beggining and End [In reply to] Can't Post

$foo = 21;
$foo = " $foo ";

now $foo has a space before and after 21.
-------------------------------------------------


duffnuts
New User

Mar 25, 2008, 5:29 PM

Post #3 of 5 (783 views)
Re: [KevinR] Add whitespace Beggining and End [In reply to] Can't Post

Thanks for your help but it doesent seem to fix what i thought it would. I should have provided a bit more info in the beggining I think.

Once I have the string in a variable I want to be able to search through a line of text which I have taken in from a file and print out any line where the user input matches with a whitespace before and after.

Currently this works for me - if($line =~ / 21 /)
However this does not - if($line =~ /$_OldVlan/) nor does if($line =~ /$OldVlan/) or many other combinations I have tried. As I want to use user input I need to be able to use a variable.

For example I want to match this line:
"the rock is 21 years old"
However I dont want to match
"the rock is 211 years old"
Nor do I want to match
"the rock is 121 years old"


eWish
User

Mar 25, 2008, 7:51 PM

Post #4 of 5 (780 views)
Re: [duffnuts] Add whitespace Beggining and End [In reply to] Can't Post

Something like this?

Code
my $word = 'there'; 
my $string = 'Hello there I am not from these parts. Are you there there?';
for ($string =~ /\s$word\s/g) {
print $_, "\n";
}



duffnuts
New User

Mar 25, 2008, 8:15 PM

Post #5 of 5 (776 views)
Re: [eWish] Add whitespace Beggining and End [In reply to] Can't Post

Thanks for you help, I had tried using \s earlier however your input has made it clear I was on the right track. What I had done wrong is not remove the endline character off the input from the user. Once I did a chop on the variable and tried your sugestion everything worked great!!! Smile

Thanks,



Duffnuts

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives