in reply to Re: number of unique characters in a string
in thread number of unique characters in a string

You're on the right track, but I think this could be made more perl-ish again ...

my $text = '0101010101'; my %hash; @hash{ split '', $text } = 1; print scalar keys %hash, "\n";

 

perl -le 'print+unpack("N",pack("B32","00000000000000000000001000110111"))'

Replies are listed 'Best First'.
Hash syntax tidbit
by crenz (Priest) on Mar 01, 2003 at 17:54 UTC

    ++ for teaching me a new hash syntax tidbit: I never knew about the

    @hash{@list_of_keys} = @list_of_values;

    syntax. Thanks!

Re: Re: Re: number of unique characters in a string
by genecutl (Beadle) on Mar 03, 2003 at 23:41 UTC
    my $text = '0101010101'; my %hash; @hash{ split '', $text } = 1; print scalar keys %hash, "\n";
    Although this code above will work correctly, it will not be doing so the way that you expected which could lead you to trouble.
    @hash{@array} = $scalar
    only sets $scalar as the value for the first key in %hash. To set all the keys, you'd want to do something like:
    @hash{@array} = ($scalar) x scalar @array;
    In the actual code above, since you don't check values anyway, you can just write:
    @hash{@array} = ();