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

Please help me with this seemingly simple problem
I have a txt file containing data in this format:
http://csdweb.unf.edu/access/htdocs/student.htm, , , 5, 0, 0, 0
I want to read each line into an array, making a multidimensional array, so I did this:

#! /usr/bin/perl -w # Open our datafile open(INPUT, '< graduate.output.txt') or die "Couldn't open the file for reading\n"; # Declare main array @MainArray = (); # build a data structure @url_info = <[INPUT]>; while(1){ print "Pick a number: "; $index = <STDIN>; if($index eq 'Q'){ last; } print "Link: $url_info[$index][0]\n"; }

But the print only displays the work Link:
Of course, this is rigged to only print the URL. But I'm still troubleshooting.
And why doesn't my loop quit if I type a Q?
Thank you very much!
-tl

Replies are listed 'Best First'.
Re: Array question II
by japhy (Canon) on Mar 14, 2001 at 20:21 UTC
    You should not have <[INPUT]>, but rather <INPUT>.

    And if you checked to see what $index held, you'd see that it has a newline at the end of it. Try:

    chomp($index = <STDIN>); if ($index eq 'Q') { ... }


    japhy -- Perl and Regex Hacker
      But that gives me an array of elements, I was hoping that each "string" would be it's own list, giving me a list of list.

      Thanks,

      -tl

        In that case, you need to split each record as you read it from the file and push a reference to the resulting array onto your main array.

        Oh, and you really should start using use strict in your code. It'll find a lot of subtle bugs.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: Array question II
by buckaduck (Chaplain) on Mar 14, 2001 at 20:56 UTC
    It's also worth pointing out that you haven't created a multidimensional array. Therefore, $url_info[$index][0] won't give you what you want.

    While we're at it, go ahead and print out the error code if the open() fails.

    Try reading the INPUT filehandle like this:

    open(INPUT, '< graduate.output.txt') or die "Couldn't open the file for reading: $!"; while(<INPUT>) { chomp; my $@a = split /,/; push(@url_info, \@a); } close(INPUT);
    buckaduck