×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Sorting with Perl ???

Sorting with Perl ???

Sorting with Perl ???

(OP)
Hi !

I have flat database:

100¦Bond¦James
101¦Smith¦Jack
102¦Black¦Janet


How can i sort this database with Surname or First Name ??
I can sort with number sort ( $a cmp $b ) @array but....


Please help !

Thanx!

RE: Sorting with Perl ???

open (FILE, $file);
my @FILE = <FILE>;
close (FILE);

my %DATABASE;
foreach my $line (@FILE)
{
  $line =~ /(\d*)¦(.*)¦(.*)/;
  my $SURNAME=$2;
  $DATABASE{$SURNAME}{"FIRSTNAME"}=$3;
  $DATABASE{$SURNAME}{"NUMBER"}=$1;
}

foreach my $surnames (sort keys(%DATABASE))
{
  print qq~$DATABASE{$surname}{"NUMBER"}¦$surname¦$DATABASE{$surname}{"FIRSTNAME"}\n~;
}

That should do it.

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Sorting with Perl ???

Or, if you'll forgive me for line counting Tom, there's this solution which uses a simple hash.

open (FILE, $file);
while(<FILE>){
    chomp;
    my ($num,$sname,$fname)=split(/\¦/);
    my $DATABASE{$sname}=$_;
}
close (FILE);

foreach my $surname (sort keys(%DATABASE)){
    print \n$DATABASE{$surname}\n";
}

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: Sorting with Perl ???

Yup, you could do that.  The only disadvantage is you cannot extract individual fields except surname.

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.

Also, you should declare: my %DATABASE;

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Sorting with Perl ???

Yes, I agree. It was the way you'd split the input line that caught my attention really.

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.

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: Sorting with Perl ???

Yeah, I've been so steeped in regexps lately that I often overlook other methods.  When all you've got is a hammer, everything begins to look like a nail.

You can make arrays of anything, including arrays.  That is a fairly well-known programming rule.  Multidimensional arrays should be covered in just about any basic programming book.

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Sorting with Perl ???

Instead of starting a new thread I'll just continue this one.  

I'm having trouble sorting myself.  I have a subroutine that prints out my directory listings recursively.  If I use the sort function it sorts alphabetically (as we all know).  However, I want to sort or print the directories first then the files underneath that directory, if you know what I mean...

Can someone help?

RE: Sorting with Perl ???

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, "$dir") or die "Can't open: $!\n";
my @alldir = sort readdir (DIR);
shift(@alldir);
shift(@alldir);
closedir(DIR);

foreach $subdir (@alldir) {

if (-d "$dir\\$subdir") {
print "\n$subdir\n\n";
recurs($dir.$subdir."\\");
}
elsif (!(-d "$dir\\$subdir")) {
print "$subdir\n";
}
}
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close