compare two string and make some operation on it?

T

Tutico

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
 
P

Paul Lalli

Ferry said:
Michele Dondi:


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
 
A

anno4000

Ferry Bolhar said:
Michele Dondi:


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
 
P

Paul Lalli

Ferry said:
Paul Lalli:

[...]

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
 
A

anno4000

Ferry Bolhar said:
Anno:


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
 
U

Uri Guttman

FB> Paul Lalli:
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
 
U

Uri Guttman

FB> Uri Guttman:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top