Computing.Net > Forums > Programming > PERL question about @lists

PERL question about @lists

Reply to Message Icon

Original Message
Name: Chris
Date: October 28, 2003 at 06:07:03 Pacific
Subject: PERL question about @lists
OS: Solaris 8
CPU/Ram: SUN
Comment:

I have created a list from which I can "pop" out certain values one by one on down the list and insert the popped value into a subroutine in a continuously looping while statement.

Problem is that if a user logs or the script is interrupted I have no way to tell the script how to pick up where it left off.

I am using the following to take the output of the list and place it in an ASCII text file on my system each time the while loop recyles immediately before the subroutine is kicked off again...

open (UPDATE, "> $file_path_variable");
print UPDATE "@cd_list";
close (UPDATE);

This continuously updates the file defined by $file_path_variable with the new elements of the list minus the element which was just popped of course.

This made sense to me I thought I was really on to something great here :) Of course it didn't work out this way :(

If I were to print the @cd_list before that operation it shows all of the values of the list seperated by spaces. If I go to the file which was opened and updated after that bit of code is executed it shows all of the elemets of the list WITHOUT spaces between them.

Now you see my dilemna...When I try to kick off the script again with a different subroutine I use the following line to define the NEW list before kicking off the subroutine again.

@continue_cd_list='more $file_path_variable';

Of course the "more" results in one very large value of list elements that are not seperated by spaces instead of individual elements.

I know there is an easier way to do this, I am new to PERL scripting and any help that can be provided would be appreciated. Perhaps I am going about this all wrong.

Thanks in advance.



Report Offensive Message For Removal


Response Number 1
Name: SN
Date: October 28, 2003 at 14:05:35 Pacific
Reply: (edit)

I've read over your question several times, and I'm not sure I understand it. I think you're saying that when you print out the array to the file, there are no spaces in between values, so when you read it back in, there is no way to determine which entry was which so you just end up with the first element of the array containing all of them. Is this the gist of it?

If so, then try printing out the elements of the array into a csv:
instead of print UPDATE "@cd_list";, try
foreach $element (@cd_list)
{
  print UPDATE "$element,";
}

Then when you read it back in,
$filecontents=<UPDATE>;
@continue_cd_list=split(/,/,$filecontents);

The above assumes, of course, that none of the array elements contain commas. If they do, then just change to a different character, ~|, etc.

I'll stop there since I don't even know if this is what you're asking, but please post back if you need more info (or just to let us know that it worked.)

Good luck,
-SN


Report Offensive Follow Up For Removal

Response Number 2
Name: FishMonger
Date: October 28, 2003 at 19:29:27 Pacific
Reply: (edit)

I'm just as confussed as SN is about what you're trying to accomplish, but here's another method to do what you're doing.

open UPDATE, "> $file_path_variable" or die "can't open $file_path_variable <$!>\n";

print UPDATE (join /\n/, @cd_list);

open UPDATE, "< $file_path_variable" or die "can't open $file_path_variable <$!>\n";

@filecontents = <UPDATE>;
close UPDATE;

This puts each element of the @cd_list on seperate lines in the "updated" file. Then reads each element back into the other array. But, I'd need to see more of your code and have a better discription of what you want to accomplish, before I can say if this is the best approach.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PERL question about @lists

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge