Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > compare two string and make some operation on it?

Reply
Thread Tools

compare two string and make some operation on it?

 
 
Tutico
Guest
Posts: n/a
 
      11-13-2006
I need to compare two string and make some operation on it?
Boot strings are same size length. In real world they are much longer then
10 characters, lenght of strings (files) is aprox 1 milion characters (1
MB). I need fastest way to compare stings and make result. If I make
comparing character by character and making operatin on string character by
character, computing is to slow to be realy usefull.

Eq.
----------------------------------------------------
# 1234567890
$a= "a...my....";
$b= "..mom....w";
$result="a.momy...w"; # operation is "OK"

but:
# 1234567890
$a= "a...my....";
$b= "..mox....w";
$result="a...my...."; # result is first string $a and operation is "NOK"
----------------------------------------------------

If is problem to use "." as empty character in Perl it is possible to use
some other caracter like "?" , space " " , "*" , "_" , "-" or some other
character.

Please help.
Thanks


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      11-13-2006
Ferry Bolhar wrote:
> Michele Dondi:
>
> > >$a= "a...my....";
> > >$b= "..mom....w";

> >
> > Incidentally, be careful when using $a and $b as general purpose
> > variables, as they're special.

>
> Well, special inside of sort blocks, OK. But else (in this example)?
>
> What's the difference between
>
> $a = "...";
> $b = "...";
>
> and
>
> $c = "...";
> $d = "...";


#!/usr/bin/perl
use strict;
use warnings;

$c = "foo";
$d = "bar";

print "$c $d\n";
__END__

Global symbol "$c" requires explicit package name at ./abcd.pl line 5.
Global symbol "$d" requires explicit package name at ./abcd.pl line 6.
Global symbol "$c" requires explicit package name at ./abcd.pl line 8.
Global symbol "$d" requires explicit package name at ./abcd.pl line 8.
Execution of ./abcd.pl aborted due to compilation errors.



#!/usr/bin/perl
use strict;
use warnings;

$a = "foo";
$b = "bar";

print "$a $b\n";
__END__

foo bar



Paul Lalli

 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-13-2006
Ferry Bolhar <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Michele Dondi:
>
> > >$a= "a...my....";
> > >$b= "..mom....w";

> >
> > Incidentally, be careful when using $a and $b as general purpose
> > variables, as they're special.

>
> Well, special inside of sort blocks, OK. But else (in this example)?
>
> What's the difference between
>
> $a = "...";
> $b = "...";
>
> and
>
> $c = "...";
> $d = "...";
>
> in a string compare operation?


If the string compare operation involves sorting you can be in trouble.

Anno
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      11-14-2006
Ferry Bolhar wrote:
> Paul Lalli:
>
> >> Well, special inside of sort blocks, OK. But else (in this example)?

>
> [...]
>
> Well, you showed us that $a and $b are excluded from 'strict vars'.
> Right, but nothing new here (in addition, within the sort block, they
> are localized and aliased to the n'th and n+1'th argument of the sort
> list for each n'th iteration over the list). So far, so well.
>
> But what has this "speciality" to do with the example given by the
> OP? Why do you not want to use $a and $b in examples like this
> one?


If you're asking what's wrong with using $a and $b in that one
particular example, the answer is "nothing". What's wrong with using
$a and $b in examples is that it's too easy to form the habbit of using
them in "real" code. I believe Michele was cautioning the OP against
developing this habbit.

Paul Lalli

 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-14-2006
Ferry Bolhar <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno:
>
> > If the string compare operation involves sorting you can be in trouble.

>
> I'm just curious to know which kind of sorting one may perform
> in a simple string compare operation, as shown by the OP.


The "specification" was

$a= "a...my....";
$b= "..mom....w";
$result="a.momy...w"; # operation is "OK"

Whatever that means exactly, it's more than a simple string compare.

More generally, one string operation that may involve sorting is checking
for anagrams.

Anno
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-14-2006
>>>>> "FB" == Ferry Bolhar <(E-Mail Removed)> writes:

FB> Paul Lalli:
>>> Well, special inside of sort blocks, OK. But else (in this example)?


FB> [...]

FB> Well, you showed us that $a and $b are excluded from 'strict vars'.
FB> Right, but nothing new here (in addition, within the sort block, they
FB> are localized and aliased to the n'th and n+1'th argument of the sort
FB> list for each n'th iteration over the list). So far, so well.

that is actually wrong. sort doesn't grab sequential pairs and sort
that. that would be a bubble sort which is very slow.

perl -le 'print sort { print "A $a B $b" ; $a <=> $b } 3, 2, 4, 1, 5'
A 3 B 2
A 4 B 1
A 2 B 1
A 2 B 4
A 3 B 4
A 1 B 5
A 5 B 2
A 5 B 3
A 5 B 4
12345

FB> But what has this "speciality" to do with the example given by the
FB> OP? Why do you not want to use $a and $b in examples like this
FB> one?

just because they are not checked under strict it is a bad idea to use
them. that is good enough. on top of that single letter var names are
bad in general as they are hard to search for and replace and have little
if any meaning (outside the math indexes like i and j). for short
examples you can use $x and $y or other names but it is just good
practice to not use $a and $b outside sort in ANY code.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      11-15-2006
>>>>> "FB" == Ferry Bolhar <(E-Mail Removed)> writes:

FB> Uri Guttman:
>> that is actually wrong. sort doesn't grab sequential pairs and sort
>> that. that would be a bubble sort which is very slow.


FB> Oh, sorry. In some ancient Perl versions (5.004 or similar), this
FB> algorithm was used, IIRC. I havn't checked this in newer versions.
FB> My apologies.

afiak, perl has never used a bubble sort. it had always used some N log
N sort supplied by libc or in the perl source.

FB> What I can't really understand is why not $A and $B were
FB> choosen instead of $a and $b. Anyone knows that uppercase
FB> names (ARGV, INC, ENV, SIG, STDERR, just to name few)
FB> have a special meanings and should never used as normal
FB> names. So when using uppercase names for $a and $b (or
FB> at least placing them in a special SORT:: namespace), any
FB> danger of mistake could be avoided.

dunno. good question to ask larry but i bet he won't know either.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
String constant in compare operation C. J. Clegg C Programming 3 11-21-2008 12:42 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
I am looking for some python script to compare two files david Python 2 11-09-2005 07:36 AM
compare two string and get the difference jyoti ASP General 0 06-04-2004 01:20 PM



Advertisments