in reply to Re: question on Arrays
in thread question on Arrays

I'm working on Windows., but I'm not sure how else the Array would be displayed if not for typing ^Z, since its an infinite loop. Please correct me ,if I'm worng.

Replies are listed 'Best First'.
Re^3: question on Arrays
by ikegami (Patriarch) on Jul 29, 2010 at 15:33 UTC

    Your loop is

    while (1) { ... }

    Ctrl-Z does not change the value "1" returns.

    As for your original question,

    my @a; while (my $val = <>) { chomp $val; push @a, $_ if !grep $_ eq $val, @a; }
    or
    my @a; my %seen; while (<>) { chomp; next if $seen{$_}++; push @a, $_; }
Re^3: question on Arrays
by derby (Abbot) on Jul 29, 2010 at 15:37 UTC

    CTRL-Z (end of file) does not break you out of your infinite loop ... so what construct would? Maybe checking if $var has a value? Maybe moving that check into your while so you do not have an infinite loop:

    while( $val = <> )
    There are many ways. Your problem has nothing to do with arrays but with how to write effective control structures. *And* there are more idiomatic ways of figuring out if a value is in an array. Iterating through the array is going to be costly at some point -- you might be better off using a hash to track what's been placed in the array (and ... this really looks like a homework problem).

    -derby