Solved

Splitting up

Posted on 2003-11-03
3
212 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
[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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 132
unable to remove invisible characters with perl 2 96
Problem with one substitution modifying another 3 78
Using Perl DBI to query oracle 3 49
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…

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