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

Replies are listed 'Best First'.
Re: Why is my array empty?
by arturo (Vicar) on Mar 14, 2001 at 20:13 UTC

    why your loop doesn't exit: note that when you read a line from <STDIN> the input you get will contain the newline; so your eq comparison fails because it tests for exactly 'Q'. Use chomp on the input or add the newline to the string (be sure to use double quotes around the "Q" if you go this way -- but the first method is safer).

    The rest will have to wait for <code> tags to be added.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Why is my array empty?
by dash2 (Hermit) on Mar 14, 2001 at 23:41 UTC
    Well, I don't think you can use the <> operator on an arrayref. It only works on filehandles (or simple scalar variables, which might contain a reference to the filehandle). Why not just <INPUT>?

    Also, if you are splitting on commas, then you need to do that:

    @lines = <INPUT>; for ( 0 .. $#lines ) { $url_info[$_] = [ split /,/ , $lines[$_] ]; }

    But maybe I am being incredibly dumb and you know clever perl tricks that I wot not of.

    dave hj~

Re: Why is my array empty?
by telesto (Novice) on Mar 15, 2001 at 05:38 UTC
    Few things:
    • close your INPUT file.
    • chomp your input.
    • split the line.
    • do some error checking on the input so you don't try to
      access what isn't there.
    Like this:

    #!/usr/bin/perl -w # Open our datafile open(INPUT, '< graduate.output.txt') or die "Couldn't open the file for reading\n"; # build a data structure @url_info = <INPUT>; close(INPUT); while(1){ print "Pick a number: "; chomp($index = <STDIN>); next if ($index >= scalar(@url_info)); if($index =~ /^q/i){ last; } @f = split(/,\s*/, $url_info[$index]); print "Link: $f[0]\n"; }