Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl newbie - help!!!

Status
Not open for further replies.

altaratz

ISP
Apr 15, 2001
73
US
I know I'm doin this really badly, and my code still doesn't work.

I trying to use a flat tab delimited file for a database for a simple shopping site.

Each record has several elements - including color, name, description, type, etc.

I trying to write a CGI that loads all records into differnt arrays or hashes so I can test against them later (like to find related products, or relevant other content)

so:

sub open_file {
$t=1;
open(MYFILE,"c:/websites/manlywear221/manlywear.com/anna_pratti/list.txt") || die "Can't open database";
@list=<MYFILE>;
while(<MYFILE>) {
chomp;
($j_number[$t], $j_type[$t], $j_type_number[$t], $j_color[$t], $j_combo[$t], $j_name[$t], $j_descrip[$t], $j_price[$t])= (split /\t/);
$t++;
}

close(MYFILE);

}

now when I try to print let's say:

print $j_color[1] - I would think that it would print the color from the 1st record read into the subroutine - but it's printing nothing - please some help!!!

 
1. You're reading all of MYFILE into the array @list. Once this is done, you've read to the end of MYFILE. Then you try to loop over the file, but since you've reached eof, your while loop does nothing. So, all your arrays are empty. Either forget @list and just loop over MYFILE, or loop over @list instead.

2. Array indexing starts at 0 in Perl, not 1. Set $t to 0 before you start populating your arrays.

 
Code:
 ($j_number[$t], $j_type[$t], $j_type_number[$t], $j_color[$t], $j_combo[$t], $j_name[$t], $j_descrip[$t], $j_price[$t])= [b]split /\t/, $_[/b];
Just for future readability
--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Try this:

Code:
sub open_file {
   %hash = ();
   open(MYFILE,"c:/websites/manlywear221/manlywear.com/anna_pratti/list.txt") || die "Can't open database\n$!";
   @list=<MYFILE>;
   close MYFILE;
   foreach $line(@list {
   chomp;
   ($j_number, $j_type, $j_type_number, $j_color, $j_combo, $j_name, $j_descrip, $j_price)= (split /\t/);
   $hash{ $j_number }{ 'j_type' => $j_type, 'j_type_number' => $j_type_number, 'j_color' => $j_color, 'j_name' => $j_name, 'j_descrip' => $j_descrip, 'j_price' => $j_price };
   }

}

I'm assuming that $j_number is a primary key within your tab delimited file.

You can then print the hash keys { 'j_number' }using:
Code:
foreach my $key (keys %hash) { 
   print "$key = $hash{$key}
   "; 
}
Not quite sure how to print the rest of the contents, I'll leave that one for you to figure out. You can then reference the hash using:
Code:
$j_name = $hash{ 123 }{ 'j_name' };
# 123 is your hash key, if you had a variable assigned:
$j_number = 123;
$j_name = $hash{ $j_number }{ 'j_name' };
I hope that's of some help, for more resources on hash, google for it. Or take a look at There's info in there about sorting hashes, and any other bits you need on hashes!

Rob Waite
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top