Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

common prefix

Posted on 2001-07-16
2
Medium Priority
?
279 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

604 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