P
Peter Stokes
I'm trying to extract a column from a flatfile database and print it
alphabetically. I can get the data out, but I can't get it to sort.
The database is pipe-separated in a plain text file and looks like
this:
Ref_no|Title|County|Another_field|And_another|And_another
1234|A Name|Devon|Data here|More data here|And more data
1234|A Name|Somerset|Data here|More data here|And more data
1234|A Name|Nottinghamshire|Data here|More data here|And more data
1234|A Name|Essex|Data here|More data here|And more data
.... and so on
My routine is as follows:
#open the file
open (INFILE, 'database.txt') or die "could not open listing file -
$!";
while (<INFILE>) {
#split the array at the separator
@out = split /\|/;
#select the field with the county names
@out = $out[2];
#clean out duplicates
@out = grep ( (($out{$_}++ < 1) || 0), @out);
#delete blank lines left by duplicates
if (@out == 1) {
print sort @out, "\n";
}
else {
print "";
}
}
#close the file to tidy up
close INFILE;
#eof
When I print out the results, I get, typically:
Devon
Somerset
Nottinghamshire
Essex
.... which is the order they appear in the database, and they haven't
sorted alphabetically. I've tried it from every angle I can think, but
I must be missing one because I can't make this result alphabetical. I
get the same result wherever I put the 'sort' command.
Thanks in anticipation
alphabetically. I can get the data out, but I can't get it to sort.
The database is pipe-separated in a plain text file and looks like
this:
Ref_no|Title|County|Another_field|And_another|And_another
1234|A Name|Devon|Data here|More data here|And more data
1234|A Name|Somerset|Data here|More data here|And more data
1234|A Name|Nottinghamshire|Data here|More data here|And more data
1234|A Name|Essex|Data here|More data here|And more data
.... and so on
My routine is as follows:
#open the file
open (INFILE, 'database.txt') or die "could not open listing file -
$!";
while (<INFILE>) {
#split the array at the separator
@out = split /\|/;
#select the field with the county names
@out = $out[2];
#clean out duplicates
@out = grep ( (($out{$_}++ < 1) || 0), @out);
#delete blank lines left by duplicates
if (@out == 1) {
print sort @out, "\n";
}
else {
print "";
}
}
#close the file to tidy up
close INFILE;
#eof
When I print out the results, I get, typically:
Devon
Somerset
Nottinghamshire
Essex
.... which is the order they appear in the database, and they haven't
sorted alphabetically. I've tried it from every angle I can think, but
I must be missing one because I can't make this result alphabetical. I
get the same result wherever I put the 'sort' command.
Thanks in anticipation