Solved

Splitting up

Posted on 2003-11-03
3
210 Views
Last Modified: 2010-03-04
I've got a script which does the following

- Split a variable into 3 three parts:

-1- if there is one or more words between " and " put them into the variable named: $Between

-2- if there is a minus character before a word put the word into the variable
named: $Minus

-3- Put all remaining words into the variable named: $Remaining.

For example:
if the main variable contains:
This is "just a test" to see -if this works "ok".

the variable
- $Between contains: just a test AND ok
- $Minus contains: if
- $Remaining contains This is to see this works.


It works in most cases, but when I fill in at my $str='a -mdw';
I get as result:
Between=
Minus=a
Remaining=a mdw

While it should be
Between=
Minus=mdw
Remaining=a

Which lines do I need to alter to make the script work properly ?
This is the script:


use strict;
use warnings;

my $Between;
my $Minus;
my $Remaining;


my $str = '"just a test" to see -if this works "ok".';
my $tmp_str;

while($str =~ /"/){
    $tmp_str = $`;
    $str = $';
    if($str =~ /"/){
        $Between .= ' AND ' if $Between;
        $Between .= $`;
        $str = $tmp_str . $';
    }
}

while($str =~ /-/){
    $tmp_str = $`;
    $str = $';
    $str =~ /\W/;
    $Minus .= ' AND ' if $Minus;
    $Minus .= $`;
    $str = $tmp_str . $';
}

$Remaining = $str;

print "Between = $Between\n";
print "Minus = $Minus\n";
print "Remaining = $Remaining\n";
0
Comment
Question by:saskia
  • 2
3 Comments
 
LVL 1

Expert Comment

by:hemaraj123
ID: 9670833

hai,

for ur expected output, change the following code

while($str =~ /-/){
   $tmp_str = $`;
   $str = $';
   $str =~ /\W/;
   $Minus .= ' AND ' if $Minus;
   $Minus .= $`;
   $str = $tmp_str . $';
}

as

while($str =~ /-/){
   $tmp_str = $`;
   $str = $';
   $str =~ /(\W|$)/;
   $Minus .= ' AND ' if $Minus;
   $Minus .= $`;
   $str = $tmp_str . $';
}

This will do.

Actually, u r searching for a non Alpha-Numeric character, but for the test case 'a -mbw', there is no such character in the string 'mbw' (what is the value of variable $str). if u have any more doubt, pls. ask.

Good Luck,

Hemraj



0
 
LVL 28

Accepted Solution

by:
FishMonger earned 75 total points
ID: 9672697
Would you be interested in a slightly different method?

my $Between;
my $Minus;
#my $Remaining;

#my $str = '"just a test" to see -if this works "ok".';
my $str='a -mdw';

$Between .= $1 while ($str =~ s/"(.*?)"//);
$Minus .= $1 while ($str =~ s/-(\w+)//);

print "$Between\n";
print "$Minus\n";
print "$str\n";
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9672791
I forgot to add a space inbetween each of the $between matches.  So, change the one line and if you want/need to remove the trailing space, add the second line.

$Between .= "$1 " while ($str =~ s/"(.*?)"//);
$Between =~ s/\s*$//;
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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