Date: Mon, 02 Jul 2001 08:38:53 -0500 From: Michael Carman Subject: Re: On The Brink.... Message-Id: <3B40796D.FC5FA5F3@home.com> Cro wrote: > > My script ranks sites. > Information is stored about each site on the list in a .dat > file, including it's current position, name and URL. > These 'member files' are stored in a directory called 'members' > (named 1.dat, 2.dat 3.dat.....) where each site has it's own file. If your script is going to rank a large number of sites, it's probably better to *not* have a seperate file for each one, but to use a DBM (database) file instead. That's another issue, though... > Within a member's file; > @data[3] = Current Position > @data[4] = SiteName > @data[5] = SiteURL Don't use a slice when you want an element. @data is an array. $data[0] is the zeroth element in an array. @data[0] is an array slice (consisting of one element). Perl will often do the Right Thing for you, but it's better to say what you mean. > To overcome this problem I will need 6 new variables: > $1SiteName - Stores Site Ranked First Name > $1SiteURL - Stores Site Ranked First URL > $2SiteName - Stores Site Ranked Second Name > $2SiteURL - Stores Site Ranked Second URL > $3SiteName - Stores Site Ranked Third Name > $3SiteUR - Stores Site Ranked Third URL Whenever you find yourself naming variables this way, think about using an array, hash, or combination thereof: #!/usr/bin/perl -w use strict; my @site; while (my $line = ) { chomp($line); my ($name, $url) = split(/\s+/, $line); push(@site, {name => $name, url => $url}); } foreach my $i (0 .. $#site) { print "Name: $site[$i]{name}\n"; print " URL: $site[$i]{url}\n"; } __DATA__ Site1 URL1 Site2 URL2 Site3 URL3 (Note: That isn't quite the most perlish way of writing it; I'm going for clarity, here.) Read the perlref manpage -- it has lots of info on creating complex data structures. Once you get your data in the right format, manipulating it should become easy. -mjc