Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re (tilly) 2: Dynamic array names

by tilly (Archbishop)
on Feb 09, 2001 at 23:24 UTC ( #57449=note: print w/replies, xml ) Need Help??


in reply to Re: Dynamic array names
in thread Dynamic array names

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.

Replies are listed 'Best First'.
Re: Re (tilly) 2: Dynamic array names
by arturo (Vicar) on Feb 09, 2001 at 23:30 UTC

    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 subtract considerable points for missing the fact that you were breaking the spec of what was asked for in at least 3 different ways! OTOH anyone who came up with this would impress me:
        my @data = map [/[^,\n]*/g], <CSV>;
        Now for shame, find at least 3 ways in which you broke the spec. (Having the wrong array name and the wrong filehandle don't count. I am looking for egregious data errors.)
      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.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://57449]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2021-09-22 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?