in reply to question on Arrays

To (not) answer your question indirectly -- why do you think entering a CTRL-Z (or CTRL-D) would exit your while loop?

-derby

Replies are listed 'Best First'.
Re^2: question on Arrays
by hari9 (Sexton) on Jul 29, 2010 at 15:29 UTC
    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.

      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, $_; }

      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