Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Splitting a string

by lampros21_7 (Scribe)
on Feb 08, 2006 at 15:44 UTC ( #528823=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks

I have been trying to figure out how to do something. Basically, i want to split a string into pieces of 2 characters each. An example:

$mystring = "perl monks" The output should be:

$my_splitted_string = "pe er rl mo on nk ks"

From this i want to show that each word should be split in 2 character words with the last character of a word being the first character of the next word.

Thanks for reading this

Replies are listed 'Best First'.
Re: Splitting a string
by kwaping (Priest) on Feb 08, 2006 at 15:56 UTC
    Here's my shot at it:
    #!/usr/bin/perl use strict; use warnings; my $string = 'perl monks'; my $space = qr/\s/; for (0 .. (length $string) - 2) { my $match = substr($string,$_,2); next if ($match =~ $space); print "$match\n"; }

    Added: This thread shows that TIMTOWDI is alive and well!
Re: Splitting a string
by ikegami (Patriarch) on Feb 08, 2006 at 16:53 UTC

    Finally, one line, and no experimental features!

    sub double_it { return join ' ', $_[0] =~ /\w(?=\w(?<=(..)))/g; }

    Alternate versions:


      Isn't that rather more complicated than necessary?

Re: Splitting a string
by LucaPette (Friar) on Feb 08, 2006 at 15:56 UTC
    My code is untested but works with your example...
    #!/usr/bin/perl use strict; use Data::Dumper; my $string='perl monks'; my @p=grep { m/[a-z]/i } split //,$string; @p=map { "$p[$_]$p[$_+1]" } 0..$#p-1; print Dumper \@p;

      Incorrect. It returns
      pe er rl lm mo on nk ks
      instead of
      pe er rl mo on nk ks

Re: Splitting a string
by Roy Johnson (Monsignor) on Feb 08, 2006 at 18:15 UTC
    my $mystring = "perl monks"; # Oops, I thought it was supposed to span words. Thanks for the heads- +up, kwaping #(my $splitted = $mystring) =~ s/(?<=.)(\S)\s*(?=\S)/$1 $1/g; (my $splitted = $mystring) =~ s/(?<=\S)(\S)(?=\S)/$1 $1/g; print $splitted;

    Caution: Contents may have been coded under pressure.
      Sorry Roy, your solution gives lm too.

Re: Splitting a string
by svenXY (Deacon) on Feb 08, 2006 at 15:56 UTC
    I do not understand the second part of your question, but for the first part (the splitting), you could do it like this:
    #!/usr/bin/perl use strict; use warnings; my $string="perl monks"; $string =~ s/\s//; my @splitted_string; my @chars=split(//, $string); for ($i=0;$i<=$#chars;$i++) { push (@splitted_string, $chars[$i].$chars[$i+1]); } my $splitted_string = join(' ', @splitted_string);

      • Your code doesn't compile due to a strict error.
      • Your code gives a warning (once the strict error is fixed).
      • Your code doesn't give the right output. (lm shouldn't appear.)
Re: Splitting a string
by graff (Chancellor) on Feb 09, 2006 at 02:38 UTC
    I think you are talking about creating a list of bigrams from text ("bigram" is a terms used a lot in "natural language processing" circles, and is a specific flavor of "n-gram"). Presumably, your next step is to look at the likelihood of occurrence for each distinct bigram (i.e. how often "pe" occurs, etc).

    So naturally there is a module that does this: Text::Ngram.

    The only thing special about your particular case is that you want to ignore spaces (most bigram analyses of text retain the spaces, because they are meaningful for text analysis). But that's fine -- just do  tr/ //d on your string before you pass it to Text::Ngram.

Re: Splitting a string
by inman (Curate) on Feb 08, 2006 at 17:05 UTC
    "perl monks" =~ /(\w{2})(?{print "$1 "})^/;
Re: Splitting a string
by mickeyn (Priest) on Feb 09, 2006 at 08:08 UTC
    Please try this:

    my $mystring = "perl monks"; my $tmpstring = $mystring; my $my_splitted_string = ""; while ($tmpstring) { $tmpstring =~ m/^(\w{2})/ and $my_splitted_string .= $1." "; $tmpstring =~ s/^.//; } print "$my_splitted_string\n";


Re: Splitting a string
by holli (Abbot) on Feb 09, 2006 at 08:44 UTC
    $_ = "perl monks"; #pe er rl mo on nk ks for ( split /\s+/ ) { $last = ""; for ( split // ) { print "$last$_ " if $last; $last=$_; } }

    holli, /regexed monk/
Re: Splitting a string
by explorer (Chaplain) on Feb 25, 2006 at 17:54 UTC
    $x = "perl monks"; for ( $i=0; $i < length($x)-1; $i++) { ($a,$b) = (split//,substr($x,$i,2)); next if $a eq " " or $b eq " "; print "$a$b "; }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-05-25 13:55 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (90 votes). Check out past polls.