[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] using one array to sort another



On Tue, Jun 29, 1999 at 07:23:36AM -0400, cetasix@pop.erols.com wrote:
> Richard,
> 
> I'm not sure if I understand this map/sort part of Perl. My results are
> coming out as follows:
> 
> 1.1|||
> 1.1.p.1|||
> 1.1.1|||
> 1.1.2|||
> 1.1.3|||
> 
> from the following alexander.cgi program. Am I not reading in the file
> correctly? I thought I was reading in the alexander.txt (listed at end) and
> building a hash array with the numbers being the keys to the family
> information being the value.
> 
> Thanks.
> 
> alan :)
> 
> ###############################alexander.cgi##################################
> #!/usr/local/bin/perl -w
> 
> use diagnostics ;
> 
> if(open(INFILE,"alexander.txt") || die "Cannot open alexander.txt: $!\n") {

This should be:

open(INFILE,"alexander.txt") || die "Cannot open alexander.txt: $!\n";

It doesn't really make sense to have a die in the conditional of an if.



> 	while (<INFILE>) {
> 		print("The full array is ");
> 		print $_;
> 		print "\n";

Too many prints...

print "The line is $_\n";


> 		@names = $_;
> 		@names = split /\|\|\|/, $_;
> 		%names = @names;

This clobbers %names each time through the loop.  Instead of reassigning
all of %names, you want to insert just a single key and value.

@fields = split /\Q|||/, $_;
$names{$fields[0]} = $fields[1];


> 		while(($key, $value) = each(%names)) {
> 			print("<B>Key is : </B> $key and <B>Value is: </B>
> $value\n");
> 		}

This prints out the hash each time through the loop of reading from the
file.  You probably want to move it outside the loop.


> 	}
> }
> 
> @compArray = ( '1' , '1.1' , '1.1.p.1' , '1.1.1' , '1.1.2' , '1.1.3' ) ;
> 
> print @compArray;

That'll print the array all squished together.

print "@compArray\n";


> @final_results = map $_ . '|||' . $names->{$_} . "\n" , @compArray ;

You never initialized the scalar variable $names.  Your values are in the
hash %names.  This is the main reason why your results had no values after
the |||'s.

@final_results = map "$_|||$names{$_}\b", @compArray;

> print @final_results ;
> 
> 1;
> 

HTH.

Ronald

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org