Solved

common prefix

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Existing perl code to be changed for ftp to sftp handling 14 194
Question about @INC variable in perl 1 58
instmodsh - listing perl modules 2 80
Perl Untar File 1 44
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

810 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