Solved

Splitting up

Posted on 2003-11-03
3
208 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
Comment Utility

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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now