Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Splitting strings into words when there are no separators

by inman (Curate)
on Sep 15, 2005 at 16:39 UTC ( [id://492321]=note: print w/replies, xml ) Need Help??


in reply to Re: Splitting strings into words when there are no separators
in thread Splitting strings into words when there are no separators

This treatment compares each entry in the dictionary to the data and stores matching positions. All of the possible matches are then reconstructed in the printwords function.

The words are read from the dictionary and compared without needing to be stored in memory.

#! /usr/bin/perl use strict; use warnings; my $data = lc "howdoesonehandlethissensibly"; #my $data ='thesuperuserwascalledderekuser'; my %foundwords; my $position; while (<>) { chomp; next unless length>2; # remove 1 and 2 letter words $position = 0; while (index($data,lc $_, $position) >= 0) { my $found = index($data, lc $_, $position); push @{$foundwords{$found}}, lc $_; $position = $found + 1; } } # Print the word combinations printwords('', -1); sub printwords { my $string = shift; my $position = shift; while (++$position < length $data) { if (exists $foundwords{$position}) { foreach my $word (sort {length $a <=> length $b} @{$foundw +ords{$position}}) { printwords (join(' ', $string, $word), $position + len +gth($word) -1); } last; } } print "$string\n" if $position == length $data; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found