Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: split versus =~

by davido (Cardinal)
on Nov 10, 2022 at 16:09 UTC ( [id://11148107]=note: print w/replies, xml ) Need Help??


in reply to split versus =~

I can't think of a case where positive lookbehind to detect a digit preceding the hyphen wouldn't work:

#!/usr/bin/env perl use strict; use warnings; use Test::More; my @tests = ( [ '1-2-3-4' => [1,2,3,4] ], [ '1--2-3-4' => [1,-2,3,4] ], [ '-1-2-3-4' => [-1,2,3,4] ], [ '1-2-3--4' => [1,2,3,-4] ], [ '-1--2--3--4' => [-1,-2,-3,-4] ], [ '15--20-25--30' => [15,-20,25,-30] ], ); foreach my $test (@tests) { my ($raw, $want) = @$test; my @got = split /(?<=\d)-/, $raw; local $" = ', '; is_deeply \@got, $want, "'$raw' => [@$want]"; } done_testing();

This produces:

ok 1 - '1-2-3-4' => [1, 2, 3, 4] ok 2 - '1--2-3-4' => [1, -2, 3, 4] ok 3 - '-1-2-3-4' => [-1, 2, 3, 4] ok 4 - '1-2-3--4' => [1, 2, 3, -4] ok 5 - '-1--2--3--4' => [-1, -2, -3, -4] ok 6 - '15--20-25--30' => [15, -20, 25, -30] 1..6

Dave

Replies are listed 'Best First'.
Re^2: split versus =~
by russlo (Novice) on Nov 10, 2022 at 17:07 UTC

    These are great and pretty similar to what I would have supplied myself if I had thought the description of the problem wasn't self-explanatory. The actual data is possibly sensitive, so I can't just copy and paste it here. The one set of cases that you didn't have was where a number was missing from the input set ( '-2-3-4' ) - but I think I will actually look for and prevent that in the SQL statement looking for the data coming from the database.

    If the positive lookbehind doesn't work then I think I'll have to blame some other unknown causing the actual issue with the outputs being unexpected. I have a meeting later to determine if there is some other unknown cause here, hopefully I can talk it through with those individuals then and arrive at a conclusion.

    Thank you.

      There's no number missing from '-2-3-4'; that's "-2, 3, 4". If it isn't, I don't think the rules can be consistent.


      Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148107]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found