Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

split this

by pike (Monk)
on Nov 06, 2001 at 17:34 UTC ( [id://123565]=perlquestion: print w/replies, xml ) Need Help??

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

Dear brethren,

here is my problem. I have a string, say 'E/stYna~ko/', and I want to split it into single letters, except '/' and '~' should be grouped with the preceding letter (they never appear at the beginning of the string) I.e. the desired output in the above case is 'E/ s t Y n a~ k o/'. Sounds like a job for split, right? Only that I can't get it to work properly. Here's what I have tried so far:

$x = 'E/stYna~ko'; print join (' ', split /(\w[^\/~])/, $x); #->E/ st Yn a~ ko / print join (' ', split /(\w(?!\/~))/, $x); #-> E / s t Y n a ~ k +o print join (' ', split /([^\/~])/, $x); #-> E / s t Y n a ~ k o print join (' ', split /(?!\/~)/, $x); #->E / s t Y n a ~ k o

Yes, I know I could first split into individual characters and then go through the array and join the '/' and '~' with the preceding characters, but can't split give me the correct solution right away?

Puzzled, pike

Replies are listed 'Best First'.
Re: split this
by davorg (Chancellor) on Nov 06, 2001 at 17:52 UTC

    Actually, it doesn't sound like a job for split. It sounds like a job for a regular expression. Try this:

    my $x = 'E/stYna~ko'; my @chars = $x =~ /(\w\W*)/g; print "@chars\n";
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      split /(?=\w)/ also works.

              - tye (but my friends call me "Tye")

      Bravo! Succinct, concise, nothing unnecessary. Well done.

      dmm

      
      You can give a man a fish and feed him for a day ...
      Or, you can teach him to fish and feed him for a lifetime
      
Re: split this
by Tyke (Pilgrim) on Nov 06, 2001 at 18:02 UTC
    I think that you _can_ do it with a split:
    perl -le "print for split /(?=[^~\/](?:.|$))/,shift" "E/sYna~ko/"
    looks as if it works on your test data.

    Update Actually that should have been

    perl -le "print for split /(?=[^~\/])/,shift" "E/sYna~ko/"
    but you saw that
Re: split this
by Elliott (Pilgrim) on Nov 06, 2001 at 17:54 UTC
    The following groups them correctly but is still inserting extra blanks.

    $x = 'E/stYna~ko'; @j=split (/(\w\W*)/, $x); foreach $j(@j){ $i++; print "$i - $j\n"; } print "\n";

    (I added the extra code for clarity of output. It was hard to see where the exrta blanks were otherwise.)

      Almost what I was looking for. I just have to grep out the empty fields:

      print join(':', grep /\S/, split /(\w\W?)/,$x); #->E/:s:t:Y:n:a~:k:o
      And yes, using blanks as delimiters in the print was a bad idea -> changed to colons.

      Thanks, pike

        { local $" = "', '"; print "'@j'\n"; }

        perlvar is your friend :)

        Update: Well, OK. Maybe not more concise then. But, maybe easier to follow?

        I think we need a shorter alias for local.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you don't talk about Perl club."

Re: split this
by BrentDax (Hermit) on Nov 07, 2001 at 00:02 UTC
    C:\WINDOWS\Desktop>perl -e "print join ' ', split /(?!\W)/, 'E/sYna~ko +/'" E/ s Y n a~ k o/

    Your example like this forgot to use a character class--it tried to match a slash followed by a tilde.

    =cut
    --Brent Dax
    There is no sig.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-19 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found