compare two string and make some operation on it?

Discussion in 'Perl Misc' started by Tutico, Nov 13, 2006.

  1. Tutico

    Tutico Guest

    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
    Tutico, Nov 13, 2006
    #1
    1. Advertising

  2. Tutico

    Paul Lalli Guest

    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
    Paul Lalli, Nov 13, 2006
    #2
    1. Advertising

  3. Tutico

    -berlin.de Guest

    Ferry Bolhar <> 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
    -berlin.de, Nov 13, 2006
    #3
  4. Tutico

    Paul Lalli Guest

    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
    Paul Lalli, Nov 14, 2006
    #4
  5. Tutico

    -berlin.de Guest

    Ferry Bolhar <> 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
    -berlin.de, Nov 14, 2006
    #5
  6. Tutico

    Uri Guttman Guest

    >>>>> "FB" == Ferry Bolhar <> 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.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 14, 2006
    #6
  7. Tutico

    Uri Guttman Guest

    >>>>> "FB" == Ferry Bolhar <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Nov 15, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. david
    Replies:
    2
    Views:
    621
    Dennis Lee Bieber
    Nov 9, 2005
  2. GenxLogic
    Replies:
    3
    Views:
    1,263
    andrewmcdonagh
    Dec 6, 2006
  3. C. J. Clegg

    String constant in compare operation

    C. J. Clegg, Nov 20, 2008, in forum: C Programming
    Replies:
    3
    Views:
    218
    C. J. Clegg
    Nov 21, 2008
  4. Buzz Lightyear
    Replies:
    10
    Views:
    1,114
    Alexander Bartolich
    Aug 12, 2009
  5. jyoti
    Replies:
    0
    Views:
    120
    jyoti
    Jun 4, 2004
Loading...

Share This Page