Okay, i understand that. Another thing is that i have another array of which i would want to remove the element in the same position as the one i remove the one from text.
$value = 0;
foreach my $text2(@text) {
if ($text2 =~ m/^The website has been restructured/) {
splice(@text,$value,1);
splice(@links,$value,1);
}
$value++;
}
}
Is there a way to do this with a loop? Cause i dont think it would be possible to know with grep which element is the one that needs removing. Thanks | [reply] [d/l] |
Use an index explicitly instead of the easily broken, implicit one you've coded. It's important to iterate in reverse so your index will still make sense after removing data. That you have these two arrays where elements exist in pairs indicates that your data structure is mismatched. This would be better served by having an array of pairs.
foreach my $ix ( reverse 0 .. $#text ) {
if ( not length $text[ $ix ] ) {
splice @text, $ix, 1;
splice @links, $ix, 1;
}
}
The previous is optimal on very recent perls and less so on anything older. This is a great reason to use a three-arg for loop. for ( my $ix = $#text; $ix >= 0; -- $ix ) {
# same loop contents.
}
| [reply] [d/l] [select] |
Sorry, thats my fault. I put the wrong loop from my file. I have updated it in my previous post now.
| [reply] |
use Data::Dumper;
....
<snip>
....
if ($text2 =~ m/^The website has been restructured/) {
print "text array before is " . Dumper(\@text, $value);
splice(@text,$value,1);
print "text array after is " . Dumper(\@text, $value);
splice(@links,$value,1);
}
This way, you can see for yourself how @text is changing, and how it's different from what you expect.
Note that you have to pass a reference to a list to the Dumper function call, since @text is one of two elements in the list of things you want to show.
Although you have to go through and uncomment/delete the Dumper lines once you've got it working, if you like actually 'seeing' changes as they happen, this may work very well for you, especially once you start using complex data structures. | [reply] [d/l] |
use Data::Dumper;
use Term::HiliteDiff 'watch';
for ( ... ) {
my $before = "text array before is " . Dumper(\@text, $value);
...
watch( $before . "text array after is " . Dumper(\@text, $value) )
+;
}
| [reply] [d/l] |
| [reply] |
my @tmp = grep { $_->[0] !~ /^The website has been restructured/ } map
+ { [$text[$_], $arr2[$_], $_] } ( 1 .. $#text );
my @text = map { $_->[0] } @tmp;
my @arr2 = map { $_->[1] } @tmp;
undef @tmp;
note - this way you create temporary structures (map & grep), so I wouldn't use it on large arrays, but since you wrote it's a 50 elements array ... I guess you can consider this sollution.
Enjoy,
Mickey | [reply] [d/l] |