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

Re: [MacPerl] sorting arrays

At 9:21 AM -0400 6/16/99, cetasix@pop.erols.com wrote:
>I am trying to sort a simple text file:

>I'm using the following code and it does not seem to sort them.
>if(open(INFILE,"names.txt") || die("Cannot open names.txt")) {
>	while (<INFILE>) {
>		@fields = $_;
>		foreach $field (@fields) {
>			@array = split /\|\|/, $field;
>			@array = sort(@array);
>			chomp($array[0],$array[1]);
>			$lastname = $array[0];
>			$firstname = $array[1];
>			print "$lastname, $firstname\n";
>		}
>	}
>	close(INFILE);
>It does not seem to work. Is there something I'm missing in the code?

You're recreating @array each time.  You are not ever building an array
that contains every line - just one line at a time.  So, there's no way to
compare line x with line y.  There's a standard perl FAQ along the lines of
'how do I sort anything'.  You should look at that.

-Jeff Lowrey

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