chrismassey
Programmer
Hello,
I have an array which contains a number of elements, some end in .htm (html pages) and others nothing (directories). I use a foreach loop to check which contain .htm and which don't. I then separate these into separate arrays. However I am having a problem with counting whilst placing the elements into their new arrays. Here is the section of code...
As you can see, the counter will add one each time the loop goes round. Therefore the two new arrays don't have their own unique counter and elements will not be placed into the array in order. For example...
@directory_contents = ('a', 'b.htm', 'c.htm', 'd', 'e.htm');
The @page_array array will = ('', 'b.htm', 'c.htm', '', 'e.htm');
But should = ('b.htm', 'c.htm', 'e.htm');
And the @directory_array array will = ('a', '', '', 'd', '');
But should = ('a', 'd');
How can I complete this task? I cannot think of a logical way.
Thank you very much,
Chris
I have an array which contains a number of elements, some end in .htm (html pages) and others nothing (directories). I use a foreach loop to check which contain .htm and which don't. I then separate these into separate arrays. However I am having a problem with counting whilst placing the elements into their new arrays. Here is the section of code...
Code:
$counter = 1;
foreach (@directory_contents) {
if ($_ =~ m/.htm/) {
@page_array[$counter] = $_;
}
else {
@directory_array[$counter] = $_;
}
$counter++;
}
As you can see, the counter will add one each time the loop goes round. Therefore the two new arrays don't have their own unique counter and elements will not be placed into the array in order. For example...
@directory_contents = ('a', 'b.htm', 'c.htm', 'd', 'e.htm');
The @page_array array will = ('', 'b.htm', 'c.htm', '', 'e.htm');
But should = ('b.htm', 'c.htm', 'e.htm');
And the @directory_array array will = ('a', '', '', 'd', '');
But should = ('a', 'd');
How can I complete this task? I cannot think of a logical way.
Thank you very much,
Chris