?
Solved

common prefix

Posted on 2001-07-16
2
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
shlomoy earned 1200 total points
ID: 6288612
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
ID: 6288622
Thanks.
working great!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Six Sigma Control Plans
Suggested Courses

762 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