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:
Array to Hash

 



PeterLove
Deleted

Jun 12, 2000, 4:50 PM

Post #1 of 5 (720 views)
Array to Hash Can't Post

Hello
What I want to do is take a file split it up then put it in an array, from there I would like to make it a hash. But Im doing something wrong, can someone please help me?

Here is what I have:

sub arraytohash {
my(@array, %hash, $key);
open(FILE, "myfile.txt");
while(<FILE> ) {
chomp($_);
@array = split(/:/, $_);
foreach $key (keys %hash) {
$hash{$array[0]} = $_;
}
}
close(FILE);
return(%hash);
}

Thanks.


dws
Deleted

Jun 12, 2000, 6:15 PM

Post #2 of 5 (720 views)
Re: Array to Hash [In reply to] Can't Post

The script never puts anything into %hash

foreach $key (keys %hash){

is a zero-trip loop. I'm guessing that instead of the loop, you really intend

$hash{$array[0]} = $_;



PeterLove
Deleted

Jun 12, 2000, 7:15 PM

Post #3 of 5 (720 views)
Re: Array to Hash [In reply to] Can't Post

Would this take all the values from the array and put them into a hash?

sub get_options {
open(OPTIONS, "options");
while(<OPTIONS> ) {
chomp($_);
@option = split(/:/, $_, 4);
$options{$option[0]} = join(":", @option);
}
close(OPTIONS);
return (%options);
}

If not, how could I fix that?
Thanks for you help Smile


dws
Deleted

Jun 12, 2000, 8:01 PM

Post #4 of 5 (720 views)
Re: Array to Hash [In reply to] Can't Post

Sure, by why rejoin when you already have the line in $_ ?


PeterLove
Deleted

Jun 12, 2000, 8:41 PM

Post #5 of 5 (720 views)
Re: Array to Hash [In reply to] Can't Post

I thought I could put it in there for I could come back for it latter. Smile

I have one last problem.

if($option->[0] == $what->[0]) {
print "<td><FONT SIZE=\"2\"></FORM><FORM name=\"options\"><SELECT NAME=\"\">";
print "<OPTION SELECTED VALUE=\"NONE\">Select</OPTION>";
unless($option->[2] != "NOINFO") {
print "<OPTION VALUE=\"$option->[1]\">$option->[2]</OPTION>";
}
print "</SELECT></td>";
}

This works, but only once. Lets say I have this in a file:
1001:5:Red:12.00
1001:3:Blue:3.50
1002:1:Red:2.50
1002:5:Blue:12.00

And on the webpage I have I have 2 products (the 1001, and 1002 are the id numbers)
The problem is It doesnt make a drop down box for product number 1001, just 1002
and it also doesnt show multiple selections in the drop down box, just "blue" and not red.

I guess its changing or adding a statement but what? I also think if it works for one
it will work for the other.

I really appreciate your help dws. I am very grateful.

Thank you,
Peter

 
 


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

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