Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Having difficulties with "split"

by andybshaker (Novice)
on Apr 26, 2015 at 21:08 UTC ( [id://1124795]=perlquestion: print w/replies, xml ) Need Help??

andybshaker has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I have an array called @Coordinates where each element looks like 1111111..2222222, 3333333..4444444, 5555555..6666666, etc. They are not all the same number, obviously--that is just an example. The point is that in each element, there are 7 or 8 numbers separated by ".." . What I need to do is make an array with elements 1111111, 2222222, 3333333, 4444444, 5555555, 6666666, etc. I tried using a foreach loop with

foreach my $C (@Coordinates){ push (@NewCoordinates, split("..",$C)};

to no avail. Does anyone have suggestions? Also, I just want to say how helpful this website is. I don't know where I'd be without it.

Replies are listed 'Best First'.
Re: Having difficulties with "split"
by LanX (Saint) on Apr 26, 2015 at 21:14 UTC
    Split takes a regex (not a string) as first argument and . is a meta character in regexes.

    Try escaping with /\.\./

    HTH :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    update
    DB<100> split "..", "1..2..3" => ("", "", "", 3) DB<101> split /\.\./, "1..2..3" => (1, 2, 3)
Re: Having difficulties with "split"
by AnomalousMonk (Archbishop) on Apr 26, 2015 at 21:15 UTC

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @coords = qw(1111111..2222222 3333333..4444444 5555555..6666666) +; dd \@coords; ;; my @new_coords = map { split m{ \.\. }xms } @coords; dd \@new_coords; " ["1111111..2222222", "3333333..4444444", "5555555..6666666"] [1111111, 2222222, 3333333, 4444444, 5555555, 6666666]
    In a regex (which is what split takes as its first argument), the  . (dot) is a metacharacter meaning "match anything except a newline unless the  /s modifier is asserted, then match anything", so  . must be escaped in some way. See perlre, perlretut.


    Give a man a fish:  <%-(-(-(-<

Re: Having difficulties with "split"
by Laurent_R (Canon) on Apr 26, 2015 at 21:17 UTC
    The dot has a special meaning in regexes.

    Try this (untested):

    foreach my $C (@Coordinates){ push (@NewCoordinates, split /\.\./, $C); };
    Je suis Charlie.
Re: Having difficulties with "split"
by MidLifeXis (Monsignor) on Apr 27, 2015 at 14:45 UTC

    Instead of escaping the dots explicitly, you could set the RE to escape special characters for you. This is useful when you may need to expand an arbitrary delimiter.

    @foo = split("\Q$delim\E", $string);

    --MidLifeXis

Re: Having difficulties with "split"
by Anonymous Monk on Apr 26, 2015 at 21:46 UTC

    General rule: when you know what you don't want, use split. When you know what you WANT, use regex.

    push (@NewCoordinates, $C =~ /\d{7,8}/g);
      # then again, even the "map" is not needed @NewCoordinates = "@Coordinates" =~ /\d+/g;
      # no need for a "for" loop @NewCoordinates = map /\d+/g, @Coordinates;
      Oops, I misread as 7 or 8 digit numbers. just use /\d+/g for the regex.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-18 14:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found