in reply to sorting a file

This is Roy Johnson's code, but changed a bit:

#!/usr/bin/perl -w use strict; my @file; while(<DATA>){ chomp; push @file,$_; } &getClients; #---------------------- sub getClients{ my @tuples = map {[split /\s+/, $_]} grep { !/^(?:#|$)/ } @file; foreach(sort {$a->[1] cmp $b->[1]} @tuples){ my ($cltnbr, $cltname, $clNum) = @$_; print "<a href='#' title='$cltnbr' name='$cltname $clNum' oncl +ick='fillText(this);'>$cltname $clNum</a><br />"; print qq|\n|; } } 1; __DATA__ 001 client 1 002 client 2 003 client 3

Which produces:

<a href='#' title='001' name='client 1' onclick='fillText(this);'>clie +nt 1</a><br /> <a href='#' title='002' name='client 2' onclick='fillText(this);'>clie +nt 2</a><br /> <a href='#' title='003' name='client 3' onclick='fillText(this);'>clie +nt 3</a><br />

Now, instead of using a global variable like the above you should pass it into the routine as a reference.

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re^2: sorting a file
by mooker (Initiate) on Oct 19, 2005 at 12:30 UTC
    many thanks for your help - I couldnt get your code to work, but got round it a different, less sophisticated way. Will study map and grep tho

    i used

    sub getClients{ my @sortedclients; my @joinedarray; foreach(@clients){ next if m/(^#|^$)/; my ($cltnbr, $cltname) = split (' ', $_); my $joined = join " ", $cltname, $cltnbr; push @joinedarray, $joined; } foreach(@sortedclients = sort @joinedarray){ my ($cltname, $cltnbr) = split (' ', $_); print "<a href='#' title='$cltnbr' name='$cltname' onclick='fi +llText(this);'>$cltname</a><br />"; } }