CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Checking if value exists in specific column of array

 



S_Shrum
User

Nov 4, 2001, 12:05 AM

Post #1 of 6 (925 views)
Checking if value exists in specific column of array Can't Post

I am writting a script that generates a unique value into a database. I am loading the database into an multi-dim array (via split), and need to set up a DO UNTIL loop that will generate numbers until a unique one is generated. The user will specify which field the values to compare are in; this will be translated into the column # of the array and stored in a variable called $in_column.

My problem is that I don't know how to structure the end condition. This is what I got so far:


Code
do {  

# generate the random number
my $randomnumber = int ( rand ( $input->param('ubound') - $input->param('lbound') + 1 ) + $input->param('lbound') )

} until $randomnumber...


The '...' is my unknown factor for "does not exist in any record in the specified $in_column of the data array"

That leads me to my second question...does it make any difference (on large arrays), to just do a full array search or limited column searching. I assume it would be faster to do it limited, but I am not aware of the overhead processing that is involved. I don't know if the benefits would be worth it.

TIA for any help.

Sean Shrum
sean@shrum.net
www.shrum.net/programming - Free Perl Scripts
www.shrum.net/soaring - Sailplane Soaring and Combat


mickymouse
stranger

Nov 4, 2001, 2:14 PM

Post #2 of 6 (916 views)
Re: Checking if value exists in specific column of array [In reply to] Can't Post

#sorry for any spelling mistakes.
hello Shean.

if i got you right, you can do that in some different ways :

1. you can make a functions that returns true/false by checking the value you pass to it. (if at the end you've generate a number - send it to &CheckFunc() which you need to create).
for Example :
do {
# generate the random number
my $randomnumber = int ( rand ( $input->param ('ubound') - $input->param('lbound') + 1 ) + $input->param('lbound') )
} while(&CheckFunc($randomnumber)) ;
# true - means that the number is already exist and the loop is back again. false break the loop.

2. you can store the values that you generate into hash that the value itself is the key :
%hash{$randnum} = '?'
and check - while(exist($hash{$randnum});
it maybe a bad idea because it looks like a waste of memory - but i'm not shure.

anyway i hope it helped...and if not - try to tell me more, i would love to help.

--mickymouse.



S_Shrum
User

Nov 4, 2001, 10:05 PM

Post #3 of 6 (911 views)
Re: Checking if value exists in specific column of array [In reply to] Can't Post

All the data in the report is loaded into an array...hmmm...I just had a thought...that could be an issue in the future. Oh, never mind.

The problem I am having has a hidden question: Is there a directly supported way to search through a specific column in an array. I know I can do a row loop and look in the column in question, but I am looking for a GREP-ish (not nessecarily GREP, but any function that does EXACT pattern-matching) like call that would do the same thing.

The main issue is I have to search the entire array everytime I generate a number...and if that number exists, generate another number and search again...and again...and again, until a unique ID is generated. If the array is large (>10,000), this can start to bog down a machine. I need a faster way.

I like the hash idea...but how to make that concept work in an array???

Thanks for the help.

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat

(This post was edited by s_shrum on Nov 4, 2001, 9:11 PM)


mickymouse
stranger

Nov 5, 2001, 3:39 PM

Post #4 of 6 (904 views)
Re: Checking if value exists in specific column of array [In reply to] Can't Post

Hey Shean.

This one should help - if i got you right....

do {
# whatever there is to do....
.
.
.
.
} while( grep /^$RandNum$/, @RandNumGenArr );

This line is testing if a rand num you've generated is in the array of generated num's before. every time Grep returns True - that means that the number was in the array and the do-while loop is still running.

The hash idea is basicly means that you have to create the approach as a hash and not as an array. if you already create/use an array - you can create a new hash and store the values, that you already have in the arr, as keys of the hash.

(-: I know I wrote tons of spelling mistakes but I hope it was understandable.

tell me if its working - and i hope it was.

--mickymouse.

P.S.
do you know how can i use icons such as smiles and others that i see in lots of posts ?
and how did you set your example in your post to be printed blue ?



S_Shrum
User

Nov 5, 2001, 5:53 PM

Post #5 of 6 (898 views)
Re: Checking if value exists in specific column of array [In reply to] Can't Post

Close, but it won't work.

This is what I currently thought of. The problem here is that the GREP call, as you have it defined above, searches EVERY column and EVERY row of the array.

There are two problems with this:

1. Another field could contain a number that matches the random number but not nesseccarily in the specific column that I am concerned with.

2. A full array search would take longer.

What I need is a GREP (or GREP like) call that searches A SPECIFIC column in EVERY row of the array. Sorta like:


Code
grep /$randNum/, @data[ ][5]; 
^

...I know that the syntax above is wrong but it kinda gives you the idea...I am looking for a open ROW (denoted above over the '^'), specific column search.

As for the smilies and color, you need to have markup turned on (I think it is by default) and use the special http:// - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


mickymouse
stranger

Nov 6, 2001, 4:12 AM

Post #6 of 6 (892 views)
Re: Checking if value exists in specific column of array [In reply to] Can't Post

Hi Sean.

sorry but i cann't find a way to do it your way.
because if that, i'll give you an advise :
create a new array with the number you've already generated. use it as global or make it accesable every time you need (i mean that - dont release the array space when you done).
now, use a call to a function within the while statment, like : while ( &CheckArr( $RandNum));
by that you get some advandeges :
1. if you have 5000 numbers like in the array, its about 10K of RamMem and its not a large space.
2. by useing this approach you keep the option of useing a better way laiter ( like - if you find a way like you wanted). when you'll find a new way, you just have to remove the function and it's calls.
3. you can also sort your array and by that - check if the number in corrent position in the array is bigger then your generated num. if so, and no matches found, it means that the number wasn't generated before and no forther checking should be done. this way you also save some unneeded proccece operations.

try that as a bandage untill you find a better way...and thats what i can find for now.

Sorry and GoodLock .Wink
Mickeymouse.

P.S. - Thanks for your Tip.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives