Sort an array and more

K

krzys-iek

Hi

I try to rewrite my AWK/BASH script (you can see it here:
www.krionix.net/perl/ - aw file)
This script reads passwd file (included there too) and displays only those
people we want.
They are placed in a nice, neat table.

You can run this script like that: ./aw 1000 1
And 1000 is a group ID, we will see only people with GID=1000.
1 is a sorting rule (sort by login name, 2 sort by name, 3 sort by surname).

So we will see sorted (by login) people with GID 1000.

OK, now I would like to rewrite this script and use *only PERL language* no
bash etc.

I have written something like this:

------------------------------------------

#!/usr/bin/perl -w

$file = 'passwd';
open(INFO, $file);

while (<INFO>)
{
chomp;
(m/^([a-zA-Z0-9_]*):x:[0-9]+:$ARGV[0]:/) && (push @ludzie_z_1000, $_);
}

# @ludzie_z_1000 = sort @ludzie_z_1000;

foreach (@ludzie_z_1000)
{
m/^([a-zA-Z0-9_]*):x:[0-9]+:$ARGV[0]:([^:,]*).*/;
print "$1 | ";
$_=$2;
m/^([^ ]*) (.*)/;
print "$1 | $2\n";
}

----------------------------------------

You can run it like that: ./script 1000


This perl script works but I got stuck...

I do not how to sort it with sort function ( I must sort it by login or name
or surname!).

Can you help me how to do that? I can not use linux sort function (as in my
../aw script... damn)

@ludzie_z_1000 = sort @ludzie_z_1000; this solves only sorting by login but
what about name and surname?:/

Next question, How can I do something like that:

printf "| %3s %2s %10s %2s %11s %2s %15s %2s \n", ile+1, "|", $1, "|", $2,
"|", $3, "|"; ile+=1;}

with perl language? You know, now my table does not look nice...

rafit | Rafal | Tomczak
serwik | S | Serwik
sq6elt | Pawel | Jarosz
tyciu | Mateusz | Tykierko

This is a neat table/array I want :)

rafit | Rafal | Tomczak
serwik | S | Serwik
sq6elt | Pawel | Jarosz
tyciu | Mateusz | Tykierko


Thank you!
 
M

Mothra

krzys-iek said:
Can you help me how to do that? I can not use linux sort function (as in my
./aw script... damn)

@ludzie_z_1000 = sort @ludzie_z_1000; this solves only sorting by login but
what about name and surname?:/
use an array of hashes (or better still, a hash of hashes) where each
hash is a user record (including username, first name, surname)
Next question, How can I do something like that:

printf "| %3s %2s %10s %2s %11s %2s %15s %2s \n", ile+1, "|", $1, "|", $2,
"|", $3, "|"; ile+=1;}

with perl language? You know, now my table does not look nice...
All the info you need is here:
# perldoc -f format

and here:
# man perlform
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,137
Messages
2,570,797
Members
47,342
Latest member
eixataze

Latest Threads

Top