Hello all,
I am trying to work with Perl here. I have a simple array of numbers, which I am looping through and performing a test on. If the test is true, I need to delete (as in totally drop) that number from the array. The problem is that the number does not necessarily correspond to it's key index, especially after the first loop.
Some code:
Code:
foreach $i ($set[1]..$set[-1]) {
if ($i % $set[0] == 0) {
delete $set[$i];
}
}
So I have 'delete set[$i]' there, but it will not work as keys are not aligned to values. I need a way to find the index based on the value. What I really need is some built in such as $set[indexof($i)]. If it matters, the array always begins as 2..x, and I need to iterate over 3..x, hence my array slice.
You may notice this array slice won't work for me either, as I don't want to iterate over every number between $set[1] and $set[-1], I really want to iterate over every number from $set[1] to the end of the array. This is done trivially in python as in: 'set[1:]' but I cannot figure out how to do it in Perl
Another issue is that I need the key indexes to reset after each iteration, will delete do this? If I have:
Code:
$set[0] = 1
$set[1] = 2
$set[2] = 3
and I delete '2', I need:
Code:
$set[0] = 1
$set[1] = 3
I have searched many online docs for Perl but have not been able to find a way to pull this off. Surely there is one? Before you tell me to use a hash, let me say the order of the numbers is vitally important to the rest of this script, so a hash will not do...
If there is no such function, I am more than willing to entertain other ideas on how to accomplish my goal, I just really can't think of any, and I am not exactly a wizard with Perl, obviously.
Thanks