source: main/tags/2.52/gsdl/perllib/sorttools.pm@ 25422

Last change on this file since 25422 was 7954, checked in by davidb, 20 years ago

Minor tweak to regular expression that modifies white space.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1###########################################################################
2#
3# sorttools.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# various subroutines to format strings
27# for sorting
28
29package sorttools;
30
31
32# format an english string for sorting
33# i.e. convert to lowercase, remove the, a or an
34# from beginning of string etc.
35sub format_string_english {
36 my ($stringref) = @_;
37
38 $$stringref =~ tr/A-Z/a-z/;
39 $$stringref =~ s/&[^\;]+\;//g;
40 $$stringref =~ s/[^a-z0-9 ]//g;
41 $$stringref =~ s/^\s*(the|a|an)\b//;
42 $$stringref =~ s/\s+/ /g;
43 $$stringref =~ s/^\s+//;
44 $$stringref =~ s/\s+$//;
45}
46
47# format an english name for sorting
48# i.e. convert to lowercase, put surname before
49# first names etc.
50sub format_string_name_english {
51 my ($stringref) = @_;
52
53 $$stringref =~ tr/A-Z/a-z/;
54 $$stringref =~ s/&\S+;//g;
55
56 my $comma_format = ($$stringref =~ m/^.+,.+$/);
57
58 $$stringref =~ s/[^a-z0-9 ]//g;
59 $$stringref =~ s/\s+/ /g;
60 $$stringref =~ s/^\s+//;
61 $$stringref =~ s/\s+$//;
62
63
64 if (!$comma_format) {
65 # No commas in name => name in 'firstname surname' format
66 # need to sort by surname
67 my @names = split / /, $$stringref;
68 my $surname = pop @names;
69 while (scalar @names && $surname =~ /^(jnr|snr)$/i) {
70 $surname = pop @names;
71 }
72 $$stringref = $surname . " " . $$stringref;
73 }
74}
75
76# takes arguments of day, month, year and converts to
77# date of form yyyymmdd. month may be full (e.g. "January"),
78# abbreviated (e.g. "Jan"), or a number (1-12). Years like "86"
79# will be assumed to be "1986".
80sub format_date {
81 my ($day, $month, $year) = @_;
82
83 my %months = ('january' => '01', 'jan' => '01', 'february' => '02', 'feb' => '02',
84 'march' => '03', 'mar' => '03', 'april' => '04', 'apr' => '04',
85 'may' => '05', 'june' => '06', 'jun' => '06', 'july' => '07',
86 'jul' => '07', 'august' => '08', 'aug' => '08', 'september' => '09',
87 'sep' => '09', 'october' => '10', 'oct' => '10', 'november' => '11',
88 'nov' => '11', 'december' => '12', 'dec' => '12');
89
90 $month =~ tr/A-Z/a-z/;
91
92 if ($day < 1) {
93 print STDERR "sorttools::format_date WARNING day $day out of range\n";
94 $day = "01";
95 } elsif ($day > 31) {
96 print STDERR "sorttools::format_date WARNING day $day out of range\n";
97 $day = "31";
98 }
99
100 $day = "0$day" if (length($day) == 1);
101
102 if ($month =~ /^\d\d?$/) {
103 if ($month < 1) {
104 print STDERR "sorttools::format_date WARNING month $month out of range\n";
105 $month = "01";
106 } elsif ($month > 12) {
107 print STDERR "sorttools::format_date WARNING month $month out of range\n";
108 $month = "12";
109 }
110 if ($month =~ /^\d$/) {
111 $month = "0" . $month;
112 }
113 } elsif (!defined $months{$month}) {
114 print STDERR "sorttools::format_date WARNING month $month out of range\n";
115 $month = "01";
116 } else {
117 $month = $months{$month};
118 }
119
120 if ($year !~ /^\d\d\d\d$/) {
121 if ($year !~ /^\d\d$/) {
122 my $newyear = 1900 + $year;
123 print STDERR "sorttools::format_date WARNING year $year assumed to be $newyear\n";
124 $year=$newyear;
125 } else {
126 print STDERR "sorttools::format_date WARNING year $year out of range - reset to 1900\n";
127 $year = "1900";
128 }
129 }
130
131 return "$year$month$day";
132}
133
134
1351;
Note: See TracBrowser for help on using the repository browser.