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: Intermediate:
quick question about arrays...

 



JFrost
Novice

Aug 27, 2001, 8:55 AM

Post #1 of 14 (2771 views)
quick question about arrays... Can't Post

I know this question is a little long, but please read.

Here is my question/problem. I have purchased a script that does zip code searches to the nearest zip code radius. (http://www.zipcodelocators.com).

I am trying to implement this into my own classifieds script. Things seem to be working, because it's picking up the zip input from my script and finding the nearest zipcode with it's script. Here is where the problem begins, I will try to explain it as clear as I can.

The script stores all the close zipcode in an array like this

@alltest=keys(%notsorted);

When I go ahead and try doing the following in my script I works, and it prints all the nearest zipcodes.

foreach $zip (@alltest) {
print "$zip";
}

But when ever I try to compare it with my database it doesn't pick it up. Allow me to explain. When I do the following in my database script:

foreach $zip (@alltest) {
if ($fields[$db{'zip'}] ne "10007") {
$not_found = 1;
}

It works just fine, I only get the records that have "10007" in the field. But when I do the following:

foreach $zip (@alltest) {
if ($fields[$db{'zip'}] ne "$zip") {
$not_found = 1;
}

No results are found. This is driving me crazy, and I can't seem to figure out why it's not working. I know "11219" is one of the values in $zip because whem I do print "$zip"; I get a bunch of zipcodes, and one of them is "11219"

Can someone please help me on figuring this out. I know it's probably something small, but I'm starting to pull the hair out of my head.

Sorry for the long question.

Thanks in adavnce



fashimpaur
User / Moderator

Aug 27, 2001, 10:23 AM

Post #2 of 14 (2765 views)
Re: quick question about arrays... [In reply to] Can't Post

Okay. I will jump on this one. Just a couple of quick questions to try to figure out what the problem is:

What is the size of @fields?
What is the value of $db{'zip'}?
What is the value of $fields[$db{'zip}]?

Also, watch your curly braces. Your examples are not showing complete blocks, so debugging is going to be difficult. If you can, for example's sake just add a closing brace at the end of each block, let us know.

Looking for your reply.


Dennis

$a="c323745335d3221214b364d545a362532582521254c3640504c37292f493759214b3635554c3040606a0",print unpack"u*",pack "h*",$a,"\n\n"


JFrost
Novice

Aug 27, 2001, 11:15 AM

Post #3 of 14 (2760 views)
Re: quick question about arrays... [In reply to] Can't Post

Thanks for the response.

When you are asking the size of @fields I am assuming you mean @alltest. The size of @alltest varies, depending on what zip code was entered. It can vary from 20 to 50.

Assuming again that you are refering to $zip when you say $db{'zip'}. The value of $zip is a list of zip codes that are within a 5 mile radius to the originally entered zip code. This could be something like this.

11203 11204 11209 11210 11213 11214 11215 11217 11218 11219 11220 11223 11225 11226 11228 11229 11230 11231 11232 11238 11245 11252

$fields[$db{'zip}] is the database value from the field zip. This value vary.

What I am trying to do is see if the database value from the field "zip" ($fields[$db{'zip}]) does not equal one of the values from $zip, then skip that line.

I hope you understand

Thanks.



mhx
Enthusiast / Moderator

Aug 27, 2001, 12:21 PM

Post #4 of 14 (2756 views)
Re: quick question about arrays... [In reply to] Can't Post

Ok, let's do some remote debugging. Please be so kind an enter the following piece of code directly before the foreach loop:

Code
use Data::Dumper; 
print Data::Dumper->Dump(
[\%notsorted, \@alltest, \@fields, \%{db}],
[qw(*notsorted *alltest *fields *db)]
);

and attach the output exactly as is to your reply.
This will make it significantly easier for us to help you.

-- Marcus


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



JFrost
Novice

Aug 27, 2001, 12:36 PM

Post #5 of 14 (2755 views)
Re: quick question about arrays... [In reply to] Can't Post

Here it is.

%notsorted = ( 11210 => '00004', 11220 => '00002', 11230 => '00003', 11203 => '00004', 11231 => '00004', 11213 => '00004', 11204 => '00002', 11232 => '00002', 11214 => '00003', 11223 => '00004', 11215 => '00003', 11252 => '00003', 11225 => '00003', 11217 => '00004', 11226 => '00003', 11245 => '00004', 11209 => '00002', 11218 => '00002', 11219 => '00001', 11228 => '00002', 11238 => '00004', 11229 => '00004' );


@alltest = ( 11203, 11204, 11209, 11210, 11213, 11214, 11215, 11217, 11218, 11219, 11220, 11223, 11225, 11226, 11228, 11229, 11230, 11231, 11232, 11238, 11245, 11252 );

@fields = ();

%db = ( 'immigration' => 31, 'name' => 11, 'searchnationality' => 51, 'searchbody' => 55, 'status' => 4, 'modification_time' => 3, 'searchminage' => 45, 'email' => 19, 'searchmaxage' => 46, 'searchgender' => 44, 'idealmatch' => 57, 'age' => 59, 'searchlanguages' => 48, 'group_modified' => 2, 'maritalstatus' => 25, 'searcheducation' => 50, 'db_id' => '0', 'grewupcity' => 34, 'url' => 20, 'occupation' => 30, 'searchimmigration' => 54, 'sitename' => 7, 'street' => 12, 'height' => 27, 'religion' => 23, 'city' => 13, 'phone' => 17, 'howlong' => 61, 'category' => 21, 'searchmarital' => 49, 'music' => 40, 'state' => 14, 'priority' => 10, 'seeking' => 32, 'livein' => 58, 'visibility' => 9, 'observance' => 24, 'adlanguage' => 8, 'children' => 26, 'ad_duration' => 5, 'times_renewed' => 6, 'narrative' => 33, 'nationality' => 22, 'goout' => 42, 'grewupcountry' => 35, 'who_modified' => 1, 'body' => 28, 'searchheight' => 56, 'searchreligion' => 52, 'cuisine' => 39, 'zip' => 15, 'country' => 16, 'gender' => 60, 'activities' => 38, 'searchziprange' => 47, 'languages' => 37, 'traits' => 36, 'display_address' => 18, 'read' => 41, 'education' => 29, 'physical' => 43, 'searchobservance' => 53, 'result' => 62 );

By the way. In case you will wonder what %notsorted, it seems that that's the distance in miles from the original zip code.

Thanks again.



(This post was edited by JFrost on Aug 27, 2001, 11:39 AM)


mhx
Enthusiast / Moderator

Aug 27, 2001, 12:42 PM

Post #6 of 14 (2754 views)
Re: quick question about arrays... [In reply to] Can't Post

So, do you have any explanation why the @fields array is empty??

-- Marcus


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



JFrost
Novice

Aug 27, 2001, 1:00 PM

Post #7 of 14 (2751 views)
Re: quick question about arrays... [In reply to] Can't Post

I don't have an explanation for that. I do know that when I use $fields[$db{'zip}] it does show me the value of the zip field from the database. So it must be valid.

Thanks



mhx
Enthusiast / Moderator

Aug 27, 2001, 1:07 PM

Post #8 of 14 (2750 views)
Re: quick question about arrays... [In reply to] Can't Post

Are you 100% sure? Please verify the output of

Code
print "@fields\n";

Do you get any warnings?

-- Marcus


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



JFrost
Novice

Aug 27, 2001, 1:18 PM

Post #9 of 14 (2747 views)
Re: quick question about arrays... [In reply to] Can't Post

I did it again. This time in a different section of the script (in one of the foreach) I got the following results.

%notsorted = ( 11210 => '00004', 11220 => '00002', 11230 => '00003', 11203 => '00004', 11231 => '00004', 11213 => '00004', 11204 => '00002', 11232 => '00002', 11214 => '00003', 11223 => '00004', 11215 => '00003', 11252 => '00003', 11225 => '00003', 11217 => '00004', 11226 => '00003', 11245 => '00004', 11209 => '00002', 11218 => '00002', 11219 => '00001', 11228 => '00002', 11238 => '00004', 11229 => '00004' );

@alltest = ( 11203, 11204, 11209, 11210, 11213, 11214, 11215, 11217, 11218, 11219, 11220, 11223, 11225, 11226, 11228, 11229, 11230, 11231, 11232, 11238, 11245, 11252 );

@fields = ( 2670, 'cool134', 'user', '07/26/2001', 'ok', 30, '0', 'Name of Your Site', 'english', '', '', 'hkjhjk hjkhk', '', '', '', 11367, '', '', '', 'jlkj@jl.com', '', 'profiles', 'Jewish', 'Other', 'Observant', 'Never Married', 'No Children', '6\'6" or taller (198)', 'Slim / Slender', 'High school diploma', 'Travel agent', 'Tourist Visa', 'Marriage', 'This is about me', 'brooklyn', 'USA', 'IntellectualArtistic', 'English~~Russian', 'Cooking~~Reading', 'Chinese~~Deli', 'Blues~~Opera', 'Fiction~~Poetry', 'Museums~~Bars', 'Bicycling~~Soccer', 'Male', 18, 58, 25, 'English~~Russian', 'Never Married~~Divorced', 'High school diploma~~Associate degree', 'Jewish', 'Judiasm', 'Not at all', 'Work Visa', 'Average', '4\'10" (147 cm)', 'this is my ideal match', '', 30, 'Male' );

%db = ( 'immigration' => 31, 'name' => 11, 'searchnationality' => 51, 'searchbody' => 55, 'status' => 4, 'modification_time' => 3, 'searchminage' => 45, 'email' => 19, 'searchmaxage' => 46, 'searchgender' => 44, 'idealmatch' => 57, 'age' => 59, 'searchlanguages' => 48, 'group_modified' => 2, 'maritalstatus' => 25, 'searcheducation' => 50, 'db_id' => '0', 'grewupcity' => 34, 'url' => 20, 'occupation' => 30, 'searchimmigration' => 54, 'sitename' => 7, 'street' => 12, 'height' => 27, 'religion' => 23, 'city' => 13, 'phone' => 17, 'howlong' => 61, 'category' => 21, 'searchmarital' => 49, 'music' => 40, 'state' => 14, 'priority' => 10, 'seeking' => 32, 'livein' => 58, 'visibility' => 9, 'observance' => 24, 'adlanguage' => 8, 'children' => 26, 'ad_duration' => 5, 'times_renewed' => 6, 'narrative' => 33, 'nationality' => 22, 'goout' => 42, 'grewupcountry' => 35, 'who_modified' => 1, 'body' => 28, 'searchheight' => 56, 'searchreligion' => 52, 'cuisine' => 39, 'zip' => 15, 'country' => 16, 'gender' => 60, 'activities' => 38, 'searchziprange' => 47, 'languages' => 37, 'traits' => 36, 'display_address' => 18, 'read' => 41, 'education' => 29, 'physical' => 43, 'searchobservance' => 53, 'result' => 62 );

I think this is the result we were looking for.

Thanks



mhx
Enthusiast / Moderator

Aug 27, 2001, 1:58 PM

Post #10 of 14 (2745 views)
Re: quick question about arrays... [In reply to] Can't Post

Yes, that looks a lot better ;-)
If you look at the output, $fields[$db{zip}] is 11367. This zip code is not contained in @alltest or as a key in %notsorted. So you can't find it.
Looking closer at your foreach loop I also realized that it's not only unneccessary, but also wrong. You set $not_found to 1 whenever the zip codes aren't equal. So $not_found will always be set if the last element of @alltests isn't $fields[$db{zip}]. You could more easily set $not_found by saying:

Code
$not_found = not exists $notsorted{$fields[$db{zip}]};

Not only is this more efficient, it also doesn't require the @alltest array.
But your other problem is that, as mentioned above, the zip code from the database isn't contained in the %notsorted hash. So $not_found will be true anyway... Sorry, but I can't help you with that because I don't know how %notsorted and @fields are assembled.

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"@_,"



JFrost
Novice

Aug 27, 2001, 7:47 PM

Post #11 of 14 (2740 views)
Re: quick question about arrays... [In reply to] Can't Post

Thank you very much for your help. This worked perfectly. The reason why the %notsorted did not contain any values that match the database is because, when I made the test for you to see, I entered a different zipcode that had nothing to do with the fields of the database. But now when using your method and I enter a valid zipcode it works like a charm.

Thanks again.



mhx
Enthusiast / Moderator

Aug 27, 2001, 8:21 PM

Post #12 of 14 (2739 views)
Re: quick question about arrays... [In reply to] Can't Post

Glad to hear it works now. I personally find Data::Dumper to be one of the most useful modules for debugging, because it allows to print arbitrarily complex data structures in a one-liner. Another nice thing is that you can directly paste its output into another Perl script, so I could directly take your output and play around with it as if I were running your script.

-- Marcus


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



JFrost
Novice

Aug 28, 2001, 3:08 PM

Post #13 of 14 (2732 views)
Re: quick question about arrays... [In reply to] Can't Post

Hi,

I'm hate to have to re-open this thread, but it looks I have another small problem.

As I mentioned, the solution you gave me works perfectly. The problem now occurs when I go to the next page results. Let me explain. The search script is set to create a navigation bar with next pages to see the rest of the results if there are more then 10 records found. As soon as I hit "Next >>", I get "no serch results found". I know there are more records, but it's not pulling it up. Why do you think this could be doing this?

p.s. The next page feature worked fine before I implemented the code you gave me.

Thanks again for all your help.



mhx
Enthusiast / Moderator

Aug 28, 2001, 11:01 PM

Post #14 of 14 (2725 views)
Re: quick question about arrays... [In reply to] Can't Post

Without seeing the whole code, I can't give you precise answer. (Not sure it I could if you posted it, but without I definetely can't.)
The only difference between my code and your original code is the following three points:
1. Mine doesn't use a foreach loop. Additional or missing foreach loops don't normally cause other script features to disappear.
2. Mine doesn't use the @alltest array anymore. If you rely on this array being initialized somewhere else in the code, this could be a reason.
3. Your's was wrong. (As I explained in an earlier post.)
To check out what's going on: There must be one (or more) condition(s) in your script that lead(s) to "No search results found". Examining these should point out why the script thinks that there are no more search results. Once you know what it's really complaining about, you can move backwards from there until you hit the root of the problem.
Since I'm neither your Browser nor your Perl interpreter, I cannot tell you what's going wrong when you 'hit "Next >>"', sorry.

-- Marcus


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


 
 


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

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