Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Substituting leading and trailing blanks

Posted on 1999-06-30
6
Medium Priority
?
271 Views
Last Modified: 2008-02-26
I'm trying to substitute leading and trailing blanks in order to enable boolean searching.  I use the following code:

if ($input{'indexinput'}=~/or/) {
      @input=split(/or/,$input{'indexinput'});
      foreach $i(@input) {
            s/^\s+//; # remove leading blanks
            s/\s+$//; # remove trailing blanks
            push(INPUTOR,$_);
      }
}
The error log shows that the substitution is not carried out and in fact the array INPUTOR remains empty.  Why?
0
Comment
Question by:wilfrieds
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 200 total points
ID: 1213666
You are not binding your regular expression to your loop variable $i, so it defualts to the $_ place holder. Either use:
....
$i =~ s/^\s+//; # remove leading blanks
$i =~ s/\s+$//; # remove trailing blanks
push(INPUTOR,$i);

OR to default to $_

foreach (@input) {
s/^\s+//; # remove leading blanks
s/\s+$//; # remove trailing blanks
push(INPUTOR,$_);


 

0
 
LVL 84

Expert Comment

by:ozo
ID: 1213667
#note that @input is modified, so you could have said
foreach( @input ){
     s/^\s+//; # remove leading blanks
     s/\s+$//; # remove trailing blanks
}
push @INPUTOR, @input;

#by the way, did you mean to say
@input=split(/\s+or\s+/,$input{indexinput});
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213668
Just noticed a typo, should have said:

push(@INPUTOR,$i);
push(@INPUTOR,$_);


 

 


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 84

Expert Comment

by:ozo
ID: 1213669
I noticed the missing @ too, but when I tried it without, it worked anyway.
Aren't prototypes amazing?
0
 
LVL 1

Author Comment

by:wilfrieds
ID: 1213670
The sequence push(INPUTOR,$_) isn't a prototype.  The camel book says the @ is optional (p.169).
Thanks for answering my question.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213671
#But the new camel book doesn't say the @ is optional, because it's not quite true.
push(@time,$_); #vs push(time,$_);
push(@s,$_); #vs push(s,$_);
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question