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: Advanced:
(ARRAYS) Open row, specific column search...

 



S_Shrum
User

Nov 7, 2001, 11:00 PM

Post #1 of 7 (3861 views)
(ARRAYS) Open row, specific column search... Can't Post

I had this posted in the BEGINNER forum...no one knew how to accomplish this:

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


Code
grep /$somevalue/, @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. Is there a way to accomplish this directly with a Perl function?

This situation I have is I am attempting to generate unique random numbers for a field of a db. Using GREP would test every value in every field. This is too much. I want to test the numbers in just 1 column of the array. Short of constructing another single cell array with the column in question, or building a hash with the data in the column, or building a loop statement and go through the db x amount of times until I generate a unique number, is there a simplier way?

TIA

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


mhx
Enthusiast

Nov 8, 2001, 9:06 AM

Post #2 of 7 (3858 views)
Re: (ARRAYS) Open row, specific column search... [In reply to] Can't Post

Wow, I've read through your posts in the beginner's forum for 5 or 6 times and now I think I've got your point. Wink
So, you have a huge database, and need a random number that is not yet contained in the nth column of any row. If that is not what you want, skip the rest of my post and forgive me. Crazy

You stated that you read the whole database into an array of arrays (rows and columns). Don't do that if you only need a single column, because it will eat up too much memory if the database grows. With your problem im mind, you should build a hash table directly while reading the database with the values in the column of interest as keys and whatever you like as value:

Code
while( <DB> ) { 
$hash{(split /$separator/)[$column]} = 1;
}

Then, your random number loop should be:

Code
$lower = $input->param('lbound'); 
$range = $input->param('ubound') - $input->param('lbound') + 1;
do { $random = $lower + int rand $range } while exists $hash{$random};

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Nov 8, 2001, 2:01 PM

Post #3 of 7 (3856 views)
Re: (ARRAYS) Open row, specific column search... [In reply to] Can't Post

That's what I need.

I guess that would work. You are correct in stating that I don't need to load the entire db...just the column that I need. I was thinking ahead to scenarios where I would have the entire db already in a array. For this script, however, this is not the case.

[deleted section - brain on vacation...I will already be at the bottom of the file when I go to append...never mind]

Thanks (again) 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 8, 2001, 1:06 PM)


S_Shrum
User

Nov 9, 2001, 12:28 AM

Post #4 of 7 (3850 views)
Re: (ARRAYS) Open row, specific column search... [In reply to] Can't Post

Okay...I'm back. While I am familiar with loading a db into an array in a WHILE loop, how do I pass the first data line to @fields and then, after getting the column number (called $in_column), how do I continue processing the rest of the data file into the hash?

Some BG info:

Since I am openning flat files, the first line in the db will have field names. I need to pass this line to a array called @fields that I will split with the supplied $delimiter.

I can then test against the values in @fields with the users $input->param('field') to get the column number that they want to compare against for the unique random number.

At this point I will know what column I need to do my hash on and also how many fields are in a record (for writing a blank record later with the unique random number in it).

TIA

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


mhx
Enthusiast

Nov 9, 2001, 2:24 AM

Post #5 of 7 (3846 views)
Re: (ARRAYS) Open row, specific column search... [In reply to] Can't Post

Just read the first row and then process all other rows:

Code
@fields = split /$separator/, <DB>; 

# determine $in_column ...

while( <DB> ) {
$hash{(split /$separator/)[$in_column]} = 1;
}

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



Kanji
User / Moderator

Nov 9, 2001, 9:49 AM

Post #6 of 7 (3843 views)
Re: (ARRAYS) Open row, specific column search... [In reply to] Can't Post

Why not use DBI and DBD::AnyData? I forget ... could you not get it working before?



S_Shrum
User

Nov 9, 2001, 10:49 AM

Post #7 of 7 (3840 views)
Re: (ARRAYS) Open row, specific column search... [In reply to] Can't Post

No...I got that working. I will be making a DBI version later. For now this is a completely self-contained set of scripts.

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

 
 


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

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