in reply to Re: Find common prefix from a list of strings
in thread Find common prefix from a list of strings
You obviously have the same must-be-a-better-way (MBABW?) feeling, and I like the idea of the grep trick, though I must admit I usually have a fear of labels. Anyway your code will work with a few minor fixes:
Update: Not silly at all bobn, I should really have specified that an empty string is the correct answer for no common prefix - thanks for pointing that out.my $s; LIST: for my $len ( 1..length $files[0]) { $s = substr($files[0],0,$len - 1); last LIST unless (scalar grep { /^$s/ } @files) == @files; } chop $s; print "prefix: $s\n";
--
I'd like to be able to assign to an luser
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Find common prefix from a list of strings
by bobn (Chaplain) on Jul 14, 2003 at 20:05 UTC | |
|
Re: Re: Re: Find common prefix from a list of strings
by bobn (Chaplain) on Jul 14, 2003 at 20:08 UTC |