Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting with Perl ???

Status
Not open for further replies.

Vesku

Programmer
Jun 7, 2000
19
0
0
FI
Hi !<br><br>I have flat database:<br><br><b>100¦Bond¦James<br>101¦Smith¦Jack<br>102¦Black¦Janet</b><br><br>How can i sort this database with Surname or First Name ??<br>I can sort with number <b>sort ( $a cmp $b ) @array</b> but....<br><br><br>Please help !<br><br>Thanx!<br><br>
 
open (FILE, $file);<br>my @FILE = &lt;FILE&gt;;<br>close (FILE);<br><br>my %DATABASE;<br>foreach my $line (@FILE)<br>{<br>&nbsp;&nbsp;$line =~ /(\d*)¦(.*)¦(.*)/;<br>&nbsp;&nbsp;my $SURNAME=$2;<br>&nbsp;&nbsp;$DATABASE{$SURNAME}{&quot;FIRSTNAME&quot;}=$3;<br>&nbsp;&nbsp;$DATABASE{$SURNAME}{&quot;NUMBER&quot;}=$1;<br>}<br><br>foreach my $surnames (sort keys(%DATABASE))<br>{<br>&nbsp;&nbsp;print qq~$DATABASE{$surname}{&quot;NUMBER&quot;}¦$surname¦$DATABASE{$surname}{&quot;FIRSTNAME&quot;}\n~;<br>}<br><br>That should do it. <p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
 
Or, if you'll forgive me for line counting Tom, there's this solution which uses a simple hash.<br><FONT FACE=monospace><b><br>open (FILE, $file);<br>while(&lt;FILE&gt;){<br>&nbsp;&nbsp;&nbsp;&nbsp;chomp;<br>&nbsp;&nbsp;&nbsp;&nbsp;my ($num,$sname,$fname)=split(/\¦/);<br>&nbsp;&nbsp;&nbsp;&nbsp;my $DATABASE{$sname}=$_;<br>}<br>close (FILE);<br><br>foreach my $surname (sort keys(%DATABASE)){<br>&nbsp;&nbsp;&nbsp;&nbsp;print \n$DATABASE{$surname}\n&quot;;<br>}<br></font></b><br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Yup, you could do that.&nbsp;&nbsp;The only disadvantage is you cannot extract individual fields except surname.<br><br>I also prefer reading the entire file into an array (if it's not too terribly large) so that I can put flocks around it and minimize the amount of time that it is open.<br><br>Also, you should declare: my %DATABASE; <p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
 
Yes, I agree. It was the way you'd split the input line that caught my attention really.<br><br>By the way -- I *do* like that array of hashes, and hash of hashes [of hashes [of hashes]] business. I'm going to use that all over the place. Seems to me that it's the easisest way of dealing with 'record' type data and it's almost skipped over in the Camel book.<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Yeah, I've been so steeped in regexps lately that I often overlook other methods.&nbsp;&nbsp;When all you've got is a hammer, everything begins to look like a nail.<br><br>You can make arrays of anything, including arrays.&nbsp;&nbsp;That is a fairly well-known programming rule.&nbsp;&nbsp;Multidimensional arrays should be covered in just about any basic programming book. <p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
 
Instead of starting a new thread I'll just continue this one.&nbsp;&nbsp;<br><br>I'm having trouble sorting myself.&nbsp;&nbsp;I have a subroutine that prints out my directory listings recursively.&nbsp;&nbsp;If I use the sort function it sorts alphabetically (as we all know).&nbsp;&nbsp;However, I want to sort or print the directories first then the files underneath that directory, if you know what I mean...<br><br>Can someone help?
 
hi,<br><br>you could make two passes through the dir<br><br>the first time - only print if -d test is true<br>the second time - only print if -d test is false<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
I did that, I think... But it still doesn't work. Here's what my code looks like...

tell me what I'm doing wrong...

sub recurs {
my ($dir) = @_;

opendir (DIR, &quot;$dir&quot;) or die &quot;Can't open: $!\n&quot;;
my @alldir = sort readdir (DIR);
shift(@alldir);
shift(@alldir);
closedir(DIR);

foreach $subdir (@alldir) {

if (-d &quot;$dir\\$subdir&quot;) {
print &quot;\n$subdir\n\n&quot;;
recurs($dir.$subdir.&quot;\\&quot;);
}
elsif (!(-d &quot;$dir\\$subdir&quot;)) {
print &quot;$subdir\n&quot;;
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top