Ask a Question related to PERL Beginners, Design and Development.
-
Benjamin Walkenhorst #1
Looking up values in arrays?
Hello everybody,
Is there a function to find out if a given value exists in an array?
What I think of is something like &has_element(\@array, $value), which
should return either a boolean value or the index containing the value
(undef if $value is not found).
I'm sure something like this exists. Or do I need to write this myself?
kind regards,
Benjamin
Benjamin Walkenhorst Guest
-
Nesting Arrays within arrays.
How do i nest an array inside another array? something like array(i).aray2(j); Can I do that? -
Matching values of an 2 arrays then move
i am pulling data via the param tags in to 2 different arrays. Here is what I want to happen: when value1 of the first array matches pullDate of... -
Why don?t the location values in the Sprite Overlay agree with the values
I would expect the left,top,right bottom values for a sprite that appear underneath it on the stage when you click on it to agree with the l,t,r,b... -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
Adding custom values and database values to DropDwonList
The other way is to create a DataTable, put the --None-- as the first Row, then read the datareader into the DataTable, and append the --other-- at... -
Paul Johnson #2
Re: Looking up values in arrays?
On Sun, Feb 15, 2004 at 08:26:08PM +0100, Benjamin Walkenhorst wrote:
Something close exists. I think I'd probably code it as:> Is there a function to find out if a given value exists in an array?
>
> What I think of is something like &has_element(\@array, $value), which
> should return either a boolean value or the index containing the value
> (undef if $value is not found).
> I'm sure something like this exists. Or do I need to write this myself?
use List::Util "first";
sub has_element
{
my ($array, $value) = @_;
first { $_ eq $value } @$array
}
--
Paul Johnson - [email]paul@pjcj.net[/email]
[url]http://www.pjcj.net[/url]
Paul Johnson Guest
-
Benjamin Walkenhorst #3
Re: Looking up values in arrays?
Hello,
On Sun, 15 Feb 2004 20:50:59 +0100
Paul Johnson <paul@pjcj.net> wrote:
Thanks a lot! =)>> > I'm sure something like this exists. Or do I need to write this
> > myself?
> Something close exists. I think I'd probably code it as:
Kind regards,
Benjamin
Benjamin Walkenhorst Guest
-
Wiggins #4
Re: Looking up values in arrays?
Benjamin Walkenhorst wrote:
This was just discussed in depth, check the archives for the last couple
of weeks. You may also want to read:
perldoc -f grep
http://danconia.org
Wiggins Guest
-
Jan #5
Re: Looking up values in arrays?
Hi,
Benjamin Walkenhorst wrote:
>>
>> Something close exists. I think I'd probably code it as:[/ref]
>
>Thanks a lot! =)
>
>Kind regards,
>
>Benjamin
>[/ref]
And you could read the extensive thread named "Array containment" I startedrecently. I got several suggestions, including mapping to a hash and usinggrep.
- Jan
--
There are 10 kinds of people: those who understand binary, and those who don't
Jan Guest
-
R. #6
Re: Looking up values in arrays?
Wiggins d'Anconia wrote:
>
> This was just discussed in depth,[/ref]
True, but this is the first time I've seen Paul's suggestion, or at least the
first time it has registered. I hope it will be offered more often, because it
is the most direct I've seen for similar needs.
Joseph
R. Guest
-
Rob #7
Re: Looking up values in arrays?
Paul Johnson wrote:
>
> Something close exists. I think I'd probably code it as:
>
> use List::Util "first";
>
> sub has_element
> {
> my ($array, $value) = @_;
> first { $_ eq $value } @$array
> }[/ref]
Except that this will return the /value/ of the element
for which the block returns true, which isn't obvious from the
call. It's identical to
sub has_element {
my ($array, $value) = @_;
foreach (@$array) {
return $_ if $_ eq $value;
}
return;
}
which, to my mind, is preferable as it is clearer what's going on. It's
certainly no slower.
Rob
Rob Guest
-
David #8
RE: Looking up values in arrays?
use List::Util "first";
sub has_element($$)
{
my( $aref, $value ) = @_;
first { $aref->[$_] eq $value } 0..$#$aref
}
?
-----Original Message-----
From: Rob Dixon [mailto:port995.com]
Sent: Monday, 16 February 2004 10:57 PM
To: org
Subject: Re: Looking up values in arrays?
Paul Johnson wrote: [/ref]
myself?
Except that this will return the /value/ of the element for which the
block returns true, which isn't obvious from the call. It's identical to
sub has_element {
my ($array, $value) = @_;
foreach (@$array) {
return $_ if $_ eq $value;
}
return;
}
which, to my mind, is preferable as it is clearer what's going on. It's
certainly no slower.
Rob
--
To unsubscribe, e-mail: org For additional
commands, e-mail: org <http://learn.perl.org/>
<http://learn.perl.org/first-response>
David Guest



Reply With Quote

