Solved

common prefix

Posted on 2001-07-16
2
249 Views
Last Modified: 2008-03-06
Hello.

I need a code to get two string of the form

a.b.c.d.f3.g9
a.b.c.w3.r6

and find the common prefix - which is, in this case, a.b.c
0
Comment
Question by:ramati
2 Comments
 
LVL 8

Accepted Solution

by:
shlomoy earned 300 total points
Comment Utility
here is a nice script to do this for you.
It's fairly easy to conver it to a function.


find_biggest_same_start.pl:
===========================
      1 #!/usr/bin/perl -w
      2 use strict;
      3 my $s1=$ARGV[0] or die "No first argument given\n";
      4 my $s2=$ARGV[1] or die "No seconds argument given\n";
      5
      6 my @s1a=split /\./,$s1;
      7 my @s2a=split /\./,$s2;
      8
      9 my $common=undef;
     10 for (my $i=0; $i<@s1a && $i<@s2a; ++$i) {
     11         print qq{\$s1a[$i]=$s1a[$i]\t\$s2a[$i]=$s2a[$i]\n};
     12         if ($s1a[$i] eq $s2a[$i]) {
     13                 $common.="." if defined $common;
     14                 $common.=$s1a[$i];
     15         }
     16 }
     17
     18 print $common,"\n";
     19
0
 

Author Comment

by:ramati
Comment Utility
Thanks.
working great!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now