Does a value exist in an array
Does a value exist in an array
am 14.11.2007 03:05:09 von M
I am pushing values to an array like so.
push @suspend_list, $account;
But before I push it to the array I would like to know if it exists already
in the array and if so not push it. From reading a few of my books I have
tried this.
if ( ! (exists($suspend_list{$account}))) {
push @suspend_list, $account;
}
It does not work. Any advice?
M
Re: Does a value exist in an array
am 14.11.2007 03:40:51 von simon.chao
On Nov 13, 10:05 pm, "M" wrote:
> I am pushing values to an array like so.
>
> push @suspend_list, $account;
>
> But before I push it to the array I would like to know if it exists already
> in the array and if so not push it. From reading a few of my books I have
> tried this.
>
> if ( ! (exists($suspend_list{$account}))) {
>
> push @suspend_list, $account;
> }
>
> It does not work. Any advice?
sounds like you want a hash. Does order matter? If not, use a hash. If
so, you can still use a Tied Hash, although I've never had to use one,
so don't know the details, although I'm sure one of the regulars will
be happy to help you out with that. But anyway, the regular (non-tied
hash) example would be:
my %suspend_list;
for my $account ( @accounts ) {
$suspend_list{$account}++;
}
....now you have all of the accounts as keys in the hash, and the
values for each key represents the number of times that key (account)
showed up. If you still want an array, you can simply:
my @suspends = keys %suspend_list;
HTH
Re: Does a value exist in an array
am 14.11.2007 03:40:58 von Jason Carlton
On Nov 13, 9:05 pm, "M" wrote:
> I am pushing values to an array like so.
>
> push @suspend_list, $account;
>
> But before I push it to the array I would like to know if it exists already
> in the array and if so not push it. From reading a few of my books I have
> tried this.
>
> if ( ! (exists($suspend_list{$account}))) {
>
> push @suspend_list, $account;
> }
>
> It does not work. Any advice?
>
> M
I had a similar problem earlier today! You're mixing up the array with
a hash.
The immediate problem is that the (!(exists... statement is looking
for a hash value, while the push... statement is setting an array. But
there's a logic problem that can be resolved fairly easily that will
fix all of that.
Forget about pushing altogether, and just add $account to a hash
instead. Like:
$suspend_list{$account} = 1; # the 1 is just a boolean character
Then when you need it:
if (exists($suspend_list{$account})) {
# do whatever you want if the value exists
}
For instance, if you were originally going to print each $account out
of the @suspend_list, like:
foreach $key (@suspend_list) { print "$key\n"; }
Then you can just do this now:
foreach $key (keys %suspend_list) { print "$key\n"; }
HTH,
Jason
Re: Does a value exist in an array
am 14.11.2007 03:54:15 von jurgenex
M wrote:
> I am pushing values to an array like so.
>
> push @suspend_list, $account;
>
> But before I push it to the array I would like to know if it exists
> already in the array and if so not push it.
Your Question is Asked Frequently. Please see "perldoc -q contains":
"How can I tell whether a list or array contains a certain element?"
> From reading a few of my
> books I have tried this.
>
> if ( ! (exists($suspend_list{$account}))) {
The array @suspend_list into which you are pushing the data has nothing to
do with the hash %suspend_list that you are trying to examine here.
> It does not work. Any advice?
See the FAQ.
jue
Re: Does a value exist in an array
am 14.11.2007 06:26:20 von xhoster
"M" wrote:
> I am pushing values to an array like so.
>
> push @suspend_list, $account;
>
> But before I push it to the array I would like to know if it exists
> already in the array and if so not push it. From reading a few of my
> books I have tried this.
>
> if ( ! (exists($suspend_list{$account}))) {
>
> push @suspend_list, $account;
> }
>
> It does not work. Any advice?
"Does not work" is an awful description.
You are using both a hash %suspend_list, and an array @suspend_list,
which happen to have the same name, apart from the sigil. I occasionally
intentionally do this, if I need functionality that neither alone will
give me, but some people find it confusing.
But you never add to the hash, just the array, so the exists will always
be false.
if ( ! (exists($suspend_list{$account}))) {
$suspend_list{$account}=undef;
push @suspend_list, $account;
}
Of course if you don't need to keep the stuff in order too, then you can
use only a hash and get rid of the array altogether.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
Re: Does a value exist in an array
am 14.11.2007 06:42:20 von bill
"Jürgen Exner" wrote in message
news:rrt_i.7297$NC.6444@trndny07...
>M wrote:
>> I am pushing values to an array like so.
>>
>> push @suspend_list, $account;
>>
>> But before I push it to the array I would like to know if it exists
>> already in the array and if so not push it.
>
> Your Question is Asked Frequently. Please see "perldoc -q contains":
"perldoc -q contain" on my system.
The module solution List::Util wins hands down for maintainabity.
I find that this is usually more important than performance.
>
> "How can I tell whether a list or array contains a certain element?"
>
>> From reading a few of my
>> books I have tried this.
>>
>> if ( ! (exists($suspend_list{$account}))) {
>
> The array @suspend_list into which you are pushing the data has nothing to
> do with the hash %suspend_list that you are trying to examine here.
>
>> It does not work. Any advice?
>
> See the FAQ.
>
> jue
>
Re: Does a value exist in an array
am 14.11.2007 07:27:12 von Peter Makholm
nolo contendere writes:
> my %suspend_list;
> for my $account ( @accounts ) {
> $suspend_list{$account}++;
> }
>
> ...now you have all of the accounts as keys in the hash, and the
> values for each key represents the number of times that key (account)
> showed up. If you still want an array, you can simply:
Note that this only works if $account is a simple value like a integer
or a string. If it is references or perl objects it stringifies the
value most often leaving you with a textual representation of the
address of the object. You can't get back to the original object from
this address.
If you need to store references you have to use something like Xho's
solution
//Makholm
Re: Does a value exist in an array
am 15.11.2007 03:32:48 von simon.chao
On Nov 14, 1:27 am, Peter Makholm wrote:
> nolo contendere writes:
> > my %suspend_list;
> > for my $account ( @accounts ) {
> > $suspend_list{$account}++;
> > }
>
> > ...now you have all of the accounts as keys in the hash, and the
> > values for each key represents the number of times that key (account)
> > showed up. If you still want an array, you can simply:
>
> Note that this only works if $account is a simple value like a integer
> or a string. If it is references or perl objects it stringifies the
> value most often leaving you with a textual representation of the
> address of the object. You can't get back to the original object from
> this address.
>
> If you need to store references you have to use something like Xho's
> solution
Very good point.
Re: Does a value exist in an array
am 19.11.2007 02:52:24 von joe
On Tue, 13 Nov 2007 20:05:09 -0600, "M"
wrote:
>I am pushing values to an array like so.
>
>push @suspend_list, $account;
>
>But before I push it to the array I would like to know if it exists already
>in the array and if so not push it. From reading a few of my books I have
>tried this.
>
>if ( ! (exists($suspend_list{$account}))) {
>
> push @suspend_list, $account;
> }
>
>It does not work. Any advice?
>
>M
>
I personally use a keys of a hash instead of an array b/c i can use
the "exists" function/option/tool..
Mostly for reading in files..
############
my $line; my %hash;
while($line = )
{
if(exists $hash{$line})
{ print "LINE: \"$line\" exists in HASH\n"; }
else { $hash{$line) = ""; }
} #EO WHILE LINE
my $key;
foreach $key (keys %hash)
{ print "UNQ HASH KEY: \"$key\"\n"; }
##########
Or translate "while($line = )" for an array..
"foreach $key (@array)".. works the same.
Joe