Solved

common prefix

Posted on 2001-07-16
2
258 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

735 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