Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im new to perl, in fact, this is my third week. My question, is probably very remedial :). Here goes, i have an array, which gets fillled with many numbers, however, only 3 Different numbers. So the array could be... 11111111 or 22222 or 333333, or 11111111222222223333333, or 11111113333333, or 222222223333333 or 1111122222.. and there is no specified number of how many 1's 2's or 3's there are. What i need to do, is somehow extract just ONE of each number, provided they exist. Is this possible? Thanks

Replies are listed 'Best First'.
Re: Array Help
by cwest (Friar) on Oct 11, 2000 at 23:41 UTC
    I think this will be my reflex to homework question... obfuscation! :-)
    print$_=>$/foreach(keys%{{map{$_=>1}@{[(1)x3=>(2)x1=>(3)x3]}}});
    Enjoy!
    --
    Casey
       I am a superhero.
    
RE: Array Help
by turnstep (Parson) on Oct 11, 2000 at 23:51 UTC

    It may be homework, but at least they are learning in a good language. :) Some intro classes use LISP. Two other ways:

    for (1..10) { push @num, (int rand 3)+1 } for (@num) { print unless $seenit{$_}++; } for $x (1..3) { print $x if grep {/$x/} @num; }
Re: Array Help
by ar0n (Priest) on Oct 11, 2000 at 22:53 UTC
    Though I won't give you the answer, I will tell you that reading perlre would be a good place to start -- in my opinion, that is.

    update: D'oh! I thought it said "... a string filled with ..."
    Please forgive my err... (you still can do it however)

    [ar0n]

      Ar0n, thanks, im reading that now :)
Re: Array Help
by kilinrax (Deacon) on Oct 11, 2000 at 22:53 UTC
    This may not be the best solution, but as ever, TMTOWTDI¹ ;-)
    #!/usr/bin/perl -w use strict; use vars qw( @array %hash $number ); @array = (1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3); print "@array\n"; foreach $number (@array){ $hash{$number}++; } @array = keys %hash; print "@array\n";

    1:There's More Than One Way To Do It
      Thanks Kilinrax, one more question... what does TMTOWTDI stand for?
        TMTOWTDI== "There's More Than One Way To Do It".

        The battle cry of Perl programmers the world over.

Re: Array Help
by Anonymous Monk on Oct 12, 2000 at 19:55 UTC
    Thanks for all your help guys :)
Re: Array Help
by Anonymous Monk on Oct 12, 2000 at 21:41 UTC
    Oh yeah.. one more thing. This is not homework. I assumed when you said homework, that you guys meant "do your homework.. learn it yourself" .. not.. that shit is my homework assignment and i need the answer. I am a student however, at Drexel University..I am on co-op and my boss wanted me to do some perl work for him, so i bought a book.. and i did the best i could. I dont even take a computer science class.... hope this clears stuff up
    Dipul Patel
Re: Array Help
by Anonymous Monk on Oct 12, 2000 at 21:41 UTC
    Oh yeah.. one more thing. This is not homework. I assumed when you said homework, that you guys meant "do your homework.. learn it yourself" .. not.. that <<THIS>> (sorry, i typed it wrong) is my homework assignment and i need the answer. I am a student however, at Drexel University..I am on co-op and my boss wanted me to do some perl work for him, so i bought a book.. and i did the best i could. I dont even take a computer science class.... hope this clears stuff up
    Dipul Patel
A reply falls below the community's threshold of quality. You may see it by logging in.