Re: removing an element without index
by afoken (Chancellor) on Aug 09, 2010 at 13:41 UTC
|
Throw a dice and use the number from the dice as index value. Use rand to avoid actually throwing a dice.
Or did you mean to remove a special element that can somehow be identified? If so, tell us how to identify the unwanted element. Also look up grep in the documentation.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] |
Re: removing an element without index
by Fletch (Bishop) on Aug 09, 2010 at 13:41 UTC
|
| [reply] |
Re: removing an element without index
by cdarke (Prior) on Aug 09, 2010 at 14:49 UTC
|
If you need to access an element by its value, then consider using a hash instead. Hashes are unordered and the keys are unique, so are not always appropriate, but your type of access is much more straightforward. It will probably be faster than searching sequentially through the array, depending on the size of the key and the exact order in the array. You can turn an array into a hash, provided the elements are unique, and delete a specific key, like this: my %hash;
my @array = (...); # whatever
@hash{@array} = undef; # RTFM to find what this does
delete $hash{'element_value'};
| [reply] [d/l] |
Re: removing an element without index
by Utilitarian (Vicar) on Aug 09, 2010 at 14:01 UTC
|
Welcome to the monastery,
Perl monks is not a code writing service but an a collective self-education project, to that end, show us what you have tried to do and you will gain more from the resulting comments.
All of the monks who have responded to you are trying to help you, even if it seems a little harsh, however posting a working solution would not advance your understanding, whereas finding the solution yourself, and learning how to find the solution yourself will allow you to develop as a programmer (pun intended)
So
- What have you tried?
- what went wrong?
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
| [reply] [d/l] |
Re: removing an element without index
by zentara (Cardinal) on Aug 09, 2010 at 15:52 UTC
|
#!/usr/bin/perl
@myarray=(1,2,3,4,5,6,7);
print "@myarray\n";
@myarray = grep { $_ != 3 && $_ != 5 } @myarray;
print "@myarray\n";
| [reply] [d/l] |
Re: removing an element without index
by Ratazong (Monsignor) on Aug 09, 2010 at 14:13 UTC
|
| [reply] |
|
|
You want to access an element from the array, therefore you need an index.
Not quite. Consider:
my @data = (1,2,3,4,5,6,7,8,9,10);
my ($element) = grep {m/4/} @data; # Get the first element containing
+the digit 4
True laziness is hard work
| [reply] [d/l] |
Re: removing an element without index
by dasgar (Priest) on Aug 09, 2010 at 23:32 UTC
|
Since you didn't provide much detail (or code), I'm making some assumptions on what you're trying to do. The example code below will "remove an element from an array without knowing its index" provided that you know what that element's value is. It may not be elegant or efficient, but it will do what you're wanting.
use strict;
my @data = (1,2,3,4,5,6,7,8,9,10);
my (@temp) = (@data); # copy array into a temporary array
undef @data; # wipeout initial array
foreach my $item (@temp) {
if ($item !~ m/4/) { # if the element's value is not 4
push @data, ($item); # copy it back into the array
} # otherwise do nothing
}
| [reply] [d/l] |
|
|
my @data = (1,2,3,4,5,6,7,8,9,10);
@data = grep {! m/4/} @data; # Remove elements containing the digit 4
True laziness is hard work
| [reply] [d/l] |
|
|
@data = grep $_ ne '4', @data; # Remove elements equal to '4'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
|
|
|
|
|
if ($item !~ m/4/) {
...
}
"If the scalar $item after stringization does not have a '4' digit anywhere in it, then do ..." would be a better description. E.g., if it's not 4, 44, 3.14159, etc.
| [reply] [d/l] [select] |
Re: removing an element without index
by JavaFan (Canon) on Aug 10, 2010 at 12:44 UTC
|
I need to remove an element from an array without knowing its index. How can I do that?
Well, which element do you want to remove? You may not have its index, but there's got to be some way of knowing which of all the elements of the array is the offending one. Please enlighten us how to indentify this mystery element.
| [reply] |