Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Check if string A and B are made up of same chars

by tybalt89 (Monsignor)
on Nov 18, 2024 at 16:23 UTC ( [id://11162789]=note: print w/replies, xml ) Need Help??


in reply to Check if string A and B are made up of same chars

And now for something completely different :)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11162756 use warnings; Test(); sub hasSameChars { ( grep(defined, @_) or return 1 ) < @_ and return 0; my %same = map { do { my @count = (0) x 256; $count[ord $&]++ while /./gs; "@count" }, 1 } @_; return %same == 1 ? 1 : 0; } sub Test { my $ERRORS = 0; # TESTING TRUE: hasSameChars("Hello World!", "Hello World!") == 1 or + $ERRORS++; hasSameChars("Hello World!", "Hdll!ooreWl ") == 1 or + $ERRORS++; hasSameChars("Hello World!", "Hdll!ooreWl ", "Hlde! Wlloor") == 1 or + $ERRORS++; hasSameChars("FALLEN", "LENFAL", "ELFALN") == 1 or + $ERRORS++; hasSameChars(undef, undef, undef) == 1 or + $ERRORS++; hasSameChars('', '', '', '') == 1 or + $ERRORS++; hasSameChars('A', 'A', 'A', 'A') == 1 or + $ERRORS++; hasSameChars("\r\n\t\0\0\1", "\0\1\t\0\n\r", "\n\1\r\t\0\0") == 1 or + $ERRORS++; hasSameChars('ABB', 'BBA', 'BAB', 'BBA') == 1 or + $ERRORS++; hasSameChars(' ', ' ', ' ', ' ') == 1 or + $ERRORS++; hasSameChars("ABC", "CAB", "BAC", "BCA", "ACB", "ABC") == 1 or + $ERRORS++; # TESTING FALSE: hasSameChars("\r\n\t\0\1\2", "\0\2\t\1\n\r", "\t\t\r\1\2\n") == 0 or + $ERRORS++; hasSameChars("Hello World!", "Hdll!ooreWl ", "Hlde! Wlloo") == 0 or + $ERRORS++; hasSameChars("Hello World!", "Hdll!ooreWl") == 0 or + $ERRORS++; hasSameChars("ABC", "C_B", "BAC", "BCA", "ACB", "ABC") == 0 or + $ERRORS++; hasSameChars("ALLEN", "ELLEN", "HALLEN", "HELLEN") == 0 or + $ERRORS++; hasSameChars("FALLEN", "Lenfal", "ELFALN") == 0 or + $ERRORS++; hasSameChars("FALLEN", "FALLEN ") == 0 or + $ERRORS++; hasSameChars(undef, "FALLEN", "FALLEN") == 0 or + $ERRORS++; hasSameChars("FALLEN", undef, "FALLEN") == 0 or + $ERRORS++; hasSameChars("FALLEN", "FALLEN", undef) == 0 or + $ERRORS++; hasSameChars('', undef, undef) == 0 or + $ERRORS++; hasSameChars('ABB', 'ABA', 'BAA', 'BAA') == 0 or + $ERRORS++; hasSameChars(' ', ' ', ' ', "\t") == 0 or + $ERRORS++; print "ERRORS = $ERRORS\n"; }

Outputs:

ERRORS = 0

Replies are listed 'Best First'.
Re^2: Check if string A and B are made up of same chars
by afoken (Chancellor) on Nov 18, 2024 at 18:44 UTC

    Well, let's see:

    X:\>perl 11162789.pl Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "1/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. ERRORS = 0 X:\>perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for MSWin32-x +64-multi-t hread Copyright 1987-2011, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. X:\>

    Line 16 is this one:

    return %same == 1 ? 1 : 0;

    Prior to Perl 5.25, a non-empty hash in scalar context returned a STRING containing used and allocated buckets, separated by a slash. After that, a hash in scalar context returns the number of keys in the hash. Obviously, you did not consider that. keys %same would always return the number of keys.

    And while we are at it, let's see what happens once you leave the 8 bit world and process Unicode strings. Adding the following line to the "TESTING FALSE" lines:

    hasSameChars("ABC\x{ABCD}", "ABC\x{CDEF}") == 0 or + $ERRORS++;
    ... Use of uninitialized value in join or string at 11162789.pl line 14. Use of uninitialized value in join or string at 11162789.pl line 14. Use of uninitialized value in join or string at 11162789.pl line 14. Use of uninitialized value in join or string at 11162789.pl line 14. Use of uninitialized value in join or string at 11162789.pl line 14. Use of uninitialized value in join or string at 11162789.pl line 14. Terminating on signal SIGBREAK(21) Use of uninitialized value in join or string at 11162789.pl line 14. Terminating on signal SIGBREAK(21) Use of uninitialized value in join or string at 11162789.pl line 14. X:\>

    Yes, it seems that it does not terminate. Actually, it would terminate, after issuing many, many warnings. Much more than I'm willing watch scrolling across my screen.

    Line 14 is this line:

    "@count"

    Let's change my change to use "ABC\x{101}" and "ABC\x{102}" to see what happens:

    ... Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. Use of uninitialized value in join or string at 11162789.pl line 14. Use of uninitialized value in join or string at 11162789.pl line 14. Use of uninitialized value in join or string at 11162789.pl line 14. Argument "2/8" isn't numeric in numeric eq (==) at 11162789.pl line 16 +. ERRORS = 0 X:\>

    Well, it seems you don't know Unicode at all, or you just completely ignored it.

    Both is sad.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Funny enough, always ERRORS = 0

      So either he just needs to silence the warnings or the OP has to work on his test suite...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

Re^2: Check if string A and B are made up of same chars
by LanX (Saint) on Nov 18, 2024 at 18:29 UTC
    Subtitles:

    Our hero is using the stringified @count array as hash key. All counts are equal IFF the hash has exactly one entry.

    Entertaining! 👏🏼

    😉

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11162789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2025-11-17 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (72 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.