Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie : Quick Split function question 2

Status
Not open for further replies.

dirtyholmes

Programmer
Mar 17, 2004
43
AU
Hi Guys,

If i wanted to split the following line

$myline = "123 456 +789-123+456 789";

into the following
123
456
+789
-123
+456
789
i.e if i hit a whitespace, a "+" sign or "-" sign i want to split at that point. Is this possible using the split
function.

I can only seem to get it to split on either whitespace, or + or - . I cant seem to get it to split using all three.

I am using something like

such as ...

@mywords = split/\S\+\-/,$myline

Am i doing something wrong, or is it better to use another func.

Regards

Mr Holmes
 
Code:
#!perl
use strict;
use warnings;

my $myline = "123 456 +789-123+456 789";
my @mywords;
push @mywords, $1 while $myline =~ /((\+|-)?\d\d\d)/g;
print qq($_\n) for (@mywords);

Output:
123
456
+789
-123
+456
789
The re matches 3 digits optionally preceded by either a plus sign or a minus sign.
(The "3 digits" could be written as \d{3}.)

Splitting on whitepace or a plus or minus as you suggest wouldn't give you the plus and minus signs in the output, and according to your post you want those.

Note that the symbol for whitespace is \s, a lowercase s. \S, uppercase S, means non-whitespace. The alternation (or) operator in regexes is the pipe symbol, |, as in the \+|- expression above.
 
$myline = "123 456 +789-123+456 789";
@numbers = $myline =~ m|[+-]?\d{3}|g;
print join ("\n", @numbers);


Kind Regards
Duncan
 
Another way tho do the work : add a space before '+' and '-' and do a simple split.
Code:
#!/usr/bin/perl -w
use strict;

my $myline = "  123    ,456 ++789-123+456 789";
my @mywords;

($_  = $myline) =~ s/([+-])/ $1/g;
@mywords = split;

print join("|n", @mywords);


Jean Pierre.
 
How about this?
Code:
$str = "123 456 +789-123+456 789";
@numbers = split(/(?:\s|(?=[+-]))/, $str);
$" = "\n";
print "@numbers\n";
Cheers, Neil
 
Actually, more accurately:
Code:
$str = "123         456 +789-123+456 789";
@numbers = split(/(?:\s+|(?=[+-]))/, $str);
$" = "\n";
print "@numbers\n";
Cheers, Neil
 
Extended patterns are very powerful.
The problem is to know how to use them...
A star for this helpful example.

Jean Pierre.
 
Neil

I hate to burst your bubble... but this is the output of your last example:-

Code:
123         456
+789
-123
+456
789

I'd have a go at fixing it... if I understood it!!!


Kind Regards
Duncan
 
whoa!

mine works!!!

Code:
$myline = "123            456 +789-123+456 789";
@numbers = $myline =~ m|[+-]?\d{3}|g;
print join ("\n", @numbers);

... lots of stars for me ;-)

output:-

Code:
123
456
+789
-123
+456
789


Kind Regards
Duncan
 
Hmm. Strange. Works fine on mine. Here's the (slightly) deobfuscated equivalent:
Code:
$str = "123         456 +789-123+456 789";
@numbers = split(/(?:        # use for clustering, not capturing
                             # otherwise split includes the delimiters
                   \s+|      # one or more whitespaces OR
                   (?=[+-])  # zero-width positive look-ahead
                             # assertion for '+' or '-'
                  )/x, $str);
$" = "\n";
print "@numbers\n";
 
The toolkit code works fine on my system (perl version 5.005_03 built for aix).

Duncan, your code gets numbers, a little bit different from spliting. ;-)

Jean Pierre.
 
Apologies Neil - it's due to the fact that when I copy / paste from the code examples sometimes I get different space characters - and that is what stopped your script working! Damn! Thought i'd caught you out... ;-)


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top