Solved

Splitting up

Posted on 2003-11-03
3
211 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
Validating the data using Oracle DBD module. 5 67
Using Perl to parse rows 7 97
Using Perl DBI to query oracle 3 46
Regular Expression for URL 10 100
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.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

789 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