Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach statement

Status
Not open for further replies.

abovebrd

IS-IT--Management
May 9, 2000
690
US
I am hoping some one can point me in the right direction.
I have a sub script that I have inculded below. In the script I have a foreach statement. The satement reads the array @data and displays each line. (That part works, cool!)

The part that I am having difficultlies with is also reading each line into a new varible. I could then edit the varible value .
$var1
$var2

etc

Any suggestion.
If I figure it out I will post back.

Thanks in advance

open(LOG,&quot;< ./data/data.txt&quot;);
@date = <LOG>;
close(LOG);

print &quot;Content-type: text/html\n\n&quot;;
$n=1;
print &quot;<UL>Payroll Log File</UL>&quot;;
foreach $messages (@data) {
print &quot;<LI>Line #$n was reported as :
$messages\n&quot;;
$n++;
}


-Danny






 
Sure, man, no problem.....

foreach $messages (@data)
{
print &quot;<LI>Line #$n was reported as :
$messages\n&quot;;
$n++;
$var_name = 'var_'.$n; # var_0, then var_1, then var_2....
$$var_name = $message; # populate $var_0, next $var_1
}

'hope this helps....


keep the rudder amid ship and beware the odd typo
 
i dont see the part where you are trying to make new variables...

but, if you want to edit them after the for loop, why don't you push them into a new array? then edit them when you want. adam@aauser.com
 
>>i dont see the part where you are trying to make new variables...

The code is not listed in the original post because I have not had any success writing it. I ma not looking for the code, but rather concepts or ideas to get me on track.

So if I push the for loop into a new array I could then read each element (at least I think thats what is called {each part of an array}) of the new array and edit as needed ?



-Danny






 
I created names for the separate vars because that was what the question asked, but, that approach is not the most concise or elegant. I believe you would do better pushing the lines into an array as luciddream suggests. Then just cycle through the array as needed.


keep the rudder amid ship and beware the odd typo
 
I hope i'm not missing something here, but couldn't you just access the elements of the array by their indexes:

[tt]$data[0] = &quot;edited&quot;;[/tt]

or if you don't want to alter the original data haphazardously, copy the original array, and edit the copied version:

[tt]my @edit_me = @data;
$edit_me[0] = &quot;altered data&quot;;
$edit_me[1] = &quot;more altered data&quot;;
map {print &quot;Line altered to: $_\n&quot;} @edit_me;[/tt]

...or whatever your code needs to do with it. I see that you wanted to make separate variable names, but accessing the elements of an array with indexes seems to work just as well for me. I'm sorry if i did miss something here and just overstated the obvious.
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Thank you for the input guys.

My perl skills are still very basic. I am learning as I go.
I phrased my question in terms of the way I was thinking.

But I am going to use the idea mention above.
Push the for loop into a new array and access the elements of the array.


One thing I noticed was that when I print the contents of the array each line has an extra carriage return. Is this what the chomp statement is used for.

Chomp the array to remove the carraiage return (\n)




-Danny






 
'chomp' is exactly what will remove the newlines, and it does work in list context, so just chomp the array away... You could instead leave out the newline in your script's print field when you return the values of the array, but while chomping and then adding it back on later is an extra step or two, i think it's easier to manage and understand.
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top