LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-20-2007, 11:15 AM   #1
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Rep: Reputation: 15
Find whether an element exists in an array.


i have an array ..

@group =qw(AA BA CA);

I want to find whether an element i give ....exists in the array..

$val ="AA";

i tried exists ..its a soalris os

it says...exists argument is not a HASH type...

What is the solution for this???

Regards
 
Old 06-20-2007, 11:35 AM   #2
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sherlock
i have an array ..

@group =qw(AA BA CA);

I want to find whether an element i give ....exists in the array..

$val ="AA";

i tried exists ..its a soalris os

it says...exists argument is not a HASH type...

What is the solution for this???

Regards

The exact error is


exists operator argument is not a HASH element
 
Old 06-20-2007, 11:49 AM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Two questions:

What language are you using?

Can you post a tiny but complete script which demonstrates the problem?
 
Old 06-20-2007, 11:42 PM   #4
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by wjevans_7d1@yahoo.co
Two questions:

What language are you using?

Can you post a tiny but complete script which demonstrates the problem?

i am using Perl 5.005 on solaris

@group =qw(AA BA CA);

$val="AA";

exists($group[$val]));


I get the below error ....on running the program
exists operator argument is not a HASH element
 
Old 06-20-2007, 11:57 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I think you might be confusing Perl "arrays" with "hashes".

You can use "exists" with a hash, but not an array.

To see if a given value exists in an array...
then you simply need to iterate through the array...
either until you find an instance of that value (there might be many)...
or until you reach the end of the array (in which case the value does *not* exist in the array).

'Hope that helps .. PSM
 
Old 06-21-2007, 12:39 AM   #6
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by paulsm4
Hi -

I think you might be confusing Perl "arrays" with "hashes".

You can use "exists" with a hash, but not an array.

To see if a given value exists in an array...
then you simply need to iterate through the array...
either until you find an instance of that value (there might be many)...
or until you reach the end of the array (in which case the value does *not* exist in the array).

'Hope that helps .. PSM

Thanks Paul...But hw can i modify the same array into a hash??
 
Old 06-21-2007, 01:55 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
you could use Perl's internal grep http://perldoc.perl.org/functions/grep.html on the array, or you can create a hash
Code:
%group = ( AA => 1,
           BA => 1,
           CA => 1
         );
I'm assuming this is just an exercise, hard-coding values like that is rare in prodn code.
Also, FYI, RH8 is waaaay old and unsupported, as is Perl 5.0.
Strongly advise you to move up to a modern distro eg the RH replacement distro Fedora core, which is now at v7(!)
Perl is now at 5.8 (per my link to the grep fn)
 
Old 06-21-2007, 02:07 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
grep /value/, @array

Code:
#!/usr/bin/perl -w

use Env;
@P = split ':', $LD_LIBRARY_PATH;

print grep /dt/, @P;
print "\n";
run it
Code:
dysp0024:primalA$ ./1.pl
/usr/dt/lib
 
Old 06-21-2007, 07:07 AM   #9
Sherlock
Member
 
Registered: Mar 2004
Location: India
Distribution: RedHat Linux 8
Posts: 60

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bigearsbilly
grep /value/, @array

Code:
#!/usr/bin/perl -w

use Env;
@P = split ':', $LD_LIBRARY_PATH;

print grep /dt/, @P;
print "\n";
run it
Code:
dysp0024:primalA$ ./1.pl
/usr/dt/lib

Ok I will try the same ....

Thanks All
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Every Element in C++ Array Points to the Same Thing anamericanjoe Programming 5 12-14-2006 08:26 PM
exchange value and key per element in associative array rblampain Programming 3 04-02-2006 09:07 AM
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM
number of element in a array of pointer? os2 Programming 7 03-09-2005 11:50 AM
C++ Six Element Array petercool Programming 2 08-20-2003 11:08 AM


All times are GMT -5. The time now is 04:31 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration