perl question

Deeko

Lifer
Jun 16, 2000
30,213
12
81
Basically I want to dynamically add rows to a 2D list.

$response_list[@response_list] = \@current_response;

that does not work. current_response is a temporary variable that is changed with the current row. I just need to somehow pass the value...kinda like saying ArrayList myList = new ArrayList(current_value) in C#. There's gotta be a way to do this?

I found something about "arrayvalues", AV datatype online, but for the life of me I can't figure out how to get them to work.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
You know....it never occurred to me to just print it out through each iteration of the loop....
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Haha, are you building a giant 2D array for no other purpose than to print it out to the console?
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
No, here is the premise...it takes a .csv file with 9 fields, and about 55000 entries....this .csv is constructed from an online survey, the fields are ID of person taking it, alot of junk I dont need, the question, the subquestion, and the answer. The result of my script is basically to cut out the sh!t and reorganize, so it looks like this:

,Q1,Q2,Q3,Q4....etc
ID, ans1, ans2, ans3, ans4....etc

This is very simplified because there are about 150 questions, and about 500 ID's answering them. Essentially I was ripping out the data i wanted with split, making a list with the current ID's answers, and then when a new ID is reached, pushing that list onto the response_list, creating a big 2D array, which i then output to a new .csv file. It never really occured to me to just output it to the file in the loop, rather than creating response_list...it solves my problem, and is much more efficient.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
While on the topic of efficiency, are you reading in the entire csv file at once, or are you reading it in one line at a time?

But yeah, building that array is a waste of memory.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
line by line.....while (<FINPUT>){ perform stuff on current response, add to current_response }
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
That'll save the most RAM. :)

But if you do ever need to build a 2D array, there was nothing wrong with the code you had. I don't know why it wasn't worknig for you.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
Well, the array was built properly. What it seems it was doing was pointing the items to the address of current_response, not copying the values. So when it got out of the loop, and I output the array to the .csv, it output 500 lines like it should, but ALL of them were what just the last one should have been.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Deeko
Well, the array was built properly. What it seems it was doing was pointing the items to the address of current_response, not copying the values. So when it got out of the loop, and I output the array to the .csv, it output 500 lines like it should, but ALL of them were what just the last one should have been.

Ah. You need to create a new instance of @current_response for each iteration through the loop.

while(1){
@current_response = (some,data);
}

will reuse the same variable for each iteration.
while(1){
my @current_response = (some,data);
}

Creates a new local array for each iteration, meaning that your arrays wont overwrite each other.
 

Deeko

Lifer
Jun 16, 2000
30,213
12
81
thought about that....but current_response is used for multiple itereations of the loop. Each iteration of the loop is one answer....current_response holds all the answers for each ID, so its used for about 150 iterations before its added to response_list(or printed).