in reply to Dynamic array names

No big shakes in Perl to do this, but you need to read up on references (perldoc perlref and perldoc perlreftut; in hard copy, I recommend Efficient Perl Programming). What you (probably) want is an *array* of references to arrays.

my @list_of_arrays; foreach (Thing) { # get array from thing push @list_of_arrays, \@array; } # to get at the arrays foreach my $array (@list_of_arrays) { # $array is a reference to an array foreach (@$array) { # etc } }

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

Replies are listed 'Best First'.
Re (tilly) 2: Dynamic array names
by tilly (Archbishop) on Feb 09, 2001 at 23:24 UTC
    Be very, very careful. You are coming close to the following snippet that I use as an interview question. Suppose that CSV is an open filehandle to a file with comma separated values. You want to extract this into an array of arrays. So you write the following snippet:
    while (<CSV>) { chomp($_); @row = split /,/, $_, -1; push @data, \@row; }
    and you get a list of copies of the last row. Why is it going wrong, and how would you fix?

    When I ask this I am willing to answer any and all questions about syntax, what constructs do, etc. I stand willing to say what the output will be as they make various modifications to the code. The point isn't familiarity with Perl, it is whether you understand how references work.

    Perhaps not surprisingly, I find that people coming from a C background (where you use pointers a lot) tend to do better on this than people who started by learning Perl for scripting/CGI/etc.

      You're right. I could have been more explicit in the original code, but I just wanted to give the gist, and so left large (and potentially important) hunks out. Of course I'd use my @array when I got it from "Thing" (which I'll assume is a line from a file which we're splitting to get an array) , à la :

      my @list_of_arrays; while (<FILE>) { my @array = split "\t", $_; push @list_of_arrays, \@array; }

      Because that gives you a (reference to a) fresh @array each time through the loop, instead of adding yet another reference to the same global array.

      As tilly has pointed out, only giving *parts* of the answer may not be helpful ... So, as they said in that long-ago decade, let's be careful out there =)

      Peevish answer to the interview question: "What, don't you people use strict around here? =)"

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

        I would give even more bonus points if the candidate was able to work without the temporary @array variable at all:
        my @list_of_arrays; while (<FILE>) { push @list_of_arrays, [ split /\t/ ]; }
        I would give bonus marks for that peevish answer (but only if you can explain why the fix mattered). :-)

        The point of this question is to see how the person debugs a simple problem. It is our responsibility to make sure that people who join learn the basic techniques we rely on to avoid them in the first place. (Including strict.)