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

[MacPerl] sorting arrays



FYI, here's the two variations on the scripts that the list helped me with.
They both work equally well for sorting the following sample list below.

> Janssen||Willem
> Hinders||Trintje
> Beving||John
> Beving||Henrietta
> Beving||Ubbe
> Beving||Henry
> Beving||Henry
> Beving||Etta
> Beving||Minnie
> Beving||Bertha
> Willemssen||male
> Willemssen||male
> Willemssen||Wopka(e)
> Brower||Coba
> Brower||John
> Brower||Bertha



SAMPLE 1

open(INFILE,"names.txt") || die("Cannot open names.txt: $!\n");
	@fields = <INFILE>;
	@fields = sort (@fields);
	chomp(@fields);
	foreach $field (@fields) {
		@array = split /\|\|/, $field;
		$lastname = $array[0];
		$firstname = $array[1];
		print "$lastname, $firstname\n";
	}
close(INFILE);



SAMPLE 2 (Note: The @sorted line is one continuous line)

open(INFILE,"names.txt") || die("Cannot open names.txt: $!\n");
@sorted = map { "$_->[0], $_->[1]" } sort { $a->[0] cmp $b->[0] || $a->[1]
cmp $b->[1] } map { chomp; [ split /\|\|/ ] } <INFILE>;
$, = "\n";
print @sorted;
close(INFILE);


Thanks to all again.

alan :)



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