Re: Splitting a string
by kwaping (Priest) on Feb 08, 2006 at 15:56 UTC
|
#!/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! | [reply] [d/l] |
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:
| [reply] [d/l] [select] |
|
/(?=(\w\w))./g;
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
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;
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
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.
| [reply] [d/l] |
|
Sorry Roy, your solution gives lm too.
fix0red!
| [reply] [d/l] |
Re: Splitting a string
by svenXY (Deacon) on Feb 08, 2006 at 15:56 UTC
|
Hi,
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);
Regards,
svenXY | [reply] [d/l] |
|
- 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.)
| [reply] [d/l] |
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. | [reply] [d/l] |
Re: Splitting a string
by inman (Curate) on Feb 08, 2006 at 17:05 UTC
|
"perl monks" =~ /(\w{2})(?{print "$1 "})^/;
| [reply] [d/l] |
Re: Splitting a string
by mickeyn (Priest) on Feb 09, 2006 at 08:08 UTC
|
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";
Enjoy,
Mickey | [reply] [d/l] |
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=$_;
}
}
| [reply] [d/l] |
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 ";
}
| [reply] [d/l] |