Solved

Splitting up

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 97
cpan issue 1 61
Perl passing in variables to do substitution 6 66
Perl script to delete older files 6 73
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

14 Experts available now in Live!

Get 1:1 Help Now