[28385] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 9749 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 21 14:10:16 2006

Date: Thu, 21 Sep 2006 11:10:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 21 Sep 2006     Volume: 10 Number: 9749

Today's topics:
    Re: Russel Quong's "Perl in 20 pages" <tadmc@augustmail.com>
    Re: Russel Quong's "Perl in 20 pages" <mritty@gmail.com>
        sort with Perl .. <tfurnitu@cisco.com>
    Re: sort with Perl .. <David.Squire@no.spam.from.here.au>
    Re: sort with Perl .. (reading news)
    Re: sort with Perl .. <David.Squire@no.spam.from.here.au>
    Re: sort with Perl .. <tadmc@augustmail.com>
    Re: sort with Perl .. <xicheng@gmail.com>
    Re: sort with Perl .. <peace.is.our.profession@gmx.de>
    Re: sort with Perl .. <xicheng@gmail.com>
    Re: sort with Perl .. (reading news)
    Re: sort with Perl .. <tfurnitu@cisco.com>
    Re: sort with Perl .. <DJStunks@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 21 Sep 2006 08:08:05 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Russel Quong's "Perl in 20 pages"
Message-Id: <slrneh53ll.ji7.tadmc@magna.augustmail.com>

Mister.Fred.Ma@gmail.com <Mister.Fred.Ma@gmail.com> wrote:
> Does anyone know where the latest version can be found?  A cached
> version of Document version 2001a is available at
> http://tinyurl.com/h6c7d


It says:

   To Perl, lists and arrays are identical

while the Perl FAQ says:

   What is the difference between a list and an array?


I would tend to believe the FAQ.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 21 Sep 2006 06:24:17 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Russel Quong's "Perl in 20 pages"
Message-Id: <1158845057.176305.230150@i3g2000cwc.googlegroups.com>

Mister.Fred.Ma@gmail.com wrote:
> Does anyone know where the latest version can be found?  A cached
> version of Document version 2001a is available at
> http://tinyurl.com/h6c7d
> (http://72.14.207.104/search?q=cache:vEXjoSaJs20J:quong.best.vwh.net/perlin20/+http://quong.best.vwh.net/perlin20/&hl=en&gl=ca&ct=clnk&cd=1).
>  It's the latest I can find, but the server hosting the original is not
> available.  I haven't seen recent references to the document on Google
> or Groups.  Thanks.

Why would you want to use this?  A cursory examination of the document
shows:
inconsistent usage of strict and warnings
inconsistent indentation
using terms "list" and "array" interchangably (Claiming that the
documentation does as well - when clearly the author has not grasped
the distinction between the terms)
Using bareword filehandles
Not checking return values of open()
Recommendations for C-Style for loops, with no mention of `for my $i
(0..$#array) { } `
Stating that undef has the "value" of the empty string
Using prototypes on subroutines
Using the English module without the '-no_match_vars' protector

 .... at that point I stopped reading.   Please don't use this document
to learn Perl.  Please?

Paul Lalli



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

Date: Thu, 21 Sep 2006 06:51:07 -0700
From: Taher <tfurnitu@cisco.com>
Subject: sort with Perl ..
Message-Id: <451298CB.BD083D77@cisco.com>


We have data in which each line has several fields , each field
seperated by tab( or anything else )  .
We want to to sort these lines of data using say the 3rd field( or any
other field )  .

Using the Unix shell sort command it is relatively straightforward as
there is an option on the delimiter and sort field , but not so in Perl
 .

Anyone want to try solving this in Perl ...



SAMPLE DATA:
xxxxx   bbb     jjjj
bbbbb   cccc    yyyyy
cccccc  ppppp   kkkk
ddddddd llllll  ssssss

( to be sorted say on the 3rd field)
FINAL RESULT SEEKED :
xxxxx   bbb     jjjj
cccccc  ppppp   kkkk
ddddddd llllll  ssssss
bbbbb   cccc    yyyyy



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

Date: Thu, 21 Sep 2006 15:02:00 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: sort with Perl ..
Message-Id: <eeu60o$cbp$1@gemini.csx.cam.ac.uk>

Taher wrote:
> We have data in which each line has several fields , each field
> seperated by tab( or anything else )  .
> We want to to sort these lines of data using say the 3rd field( or any
> other field )  .
> 
> Using the Unix shell sort command it is relatively straightforward as
> there is an option on the delimiter and sort field , but not so in Perl
> .
> 
> Anyone want to try solving this in Perl ...

How about you? Show us what you have tried, and people here will help
you to get it working.


DS


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

Date: Thu, 21 Sep 2006 14:10:46 GMT
From: "Mumia W. (reading news)" <paduille.4058.mumia.w@earthlink.net>
Subject: Re: sort with Perl ..
Message-Id: <G3xQg.10482$v%4.2303@newsread1.news.pas.earthlink.net>

On 09/21/06 08:51, Taher wrote:
> We have data in which each line has several fields , each field
> seperated by tab( or anything else )  .
> We want to to sort these lines of data using say the 3rd field( or any
> other field )  .
> 
> Using the Unix shell sort command it is relatively straightforward as
> there is an option on the delimiter and sort field , but not so in Perl
> ..
> 
> Anyone want to try solving this in Perl ...
> 
> 
> 
> SAMPLE DATA:
> xxxxx   bbb     jjjj
> bbbbb   cccc    yyyyy
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
> 
> ( to be sorted say on the 3rd field)
> FINAL RESULT SEEKED :
> xxxxx   bbb     jjjj
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
> bbbbb   cccc    yyyyy
> 

In Perl it's relatively straightforward too, if you use Sort::Fields 
from CPAN:

     use strict;
     use warnings;
     use Sort::Fields;

     my @data = q{
     xxxxx   bbb     jjjj
     bbbbb   cccc    yyyyy
     cccccc  ppppp   kkkk
     ddddddd llllll  ssssss
     } =~ m/^\s*(\S.*?)\s*$/mg;

     print "---Data:\n", join("\n", @data), "\n";
     my @sorted = fieldsort [3], @data; # sort on third field
     print "---Sorted:\n", join("\n", @sorted), "\n";


-- 
paduille.4058.mumia.w@earthlink.net


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

Date: Thu, 21 Sep 2006 15:19:49 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: sort with Perl ..
Message-Id: <eeu725$epm$1@gemini.csx.cam.ac.uk>

Mumia W. (reading news) wrote:
> On 09/21/06 08:51, Taher wrote:
>> We have data in which each line has several fields , each field
>> seperated by tab( or anything else )  .
>> We want to to sort these lines of data using say the 3rd field( or any
>> other field )  .
>>
>> Using the Unix shell sort command it is relatively straightforward as
>> there is an option on the delimiter and sort field , but not so in Perl
>> ..
>>
>> Anyone want to try solving this in Perl ...
>>
>>
>>
>> SAMPLE DATA:
>> xxxxx   bbb     jjjj
>> bbbbb   cccc    yyyyy
>> cccccc  ppppp   kkkk
>> ddddddd llllll  ssssss
>>
>> ( to be sorted say on the 3rd field)
>> FINAL RESULT SEEKED :
>> xxxxx   bbb     jjjj
>> cccccc  ppppp   kkkk
>> ddddddd llllll  ssssss
>> bbbbb   cccc    yyyyy
>>
> 
> In Perl it's relatively straightforward too, if you use Sort::Fields
> from CPAN:
> 
>     use strict;
>     use warnings;
>     use Sort::Fields;
> 
>     my @data = q{
>     xxxxx   bbb     jjjj
>     bbbbb   cccc    yyyyy
>     cccccc  ppppp   kkkk
>     ddddddd llllll  ssssss
>     } =~ m/^\s*(\S.*?)\s*$/mg;
> 
>     print "---Data:\n", join("\n", @data), "\n";
>     my @sorted = fieldsort [3], @data; # sort on third field
>     print "---Sorted:\n", join("\n", @sorted), "\n";
> 
> 

What happened to waiting for the OP to at least make an attempt? If
we're just doing folks work for them today, here's a moduleless version:

----

#!/usr/bin/perl

use strict;
use warnings;

my @records;
while (<DATA>) {
	chomp;
	my @record = split /\s+/;
	push  @records, \@record;
}

my $sort_field = 2; # i.e. the third field
for my $record_ref (sort {$a->[$sort_field] cmp $b->[$sort_field]} @records)
{
	print join ("\t", @$record_ref), "\n"; # parentheses for clarity
}

__DATA__
xxxxx   bbb     jjjj
bbbbb   cccc    yyyyy
cccccc  ppppp   kkkk
ddddddd llllll  ssssss

----

Output:

xxxxx   bbb     jjjj
cccccc  ppppp   kkkk
ddddddd llllll  ssssss
bbbbb   cccc    yyyyy

----


DS


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

Date: Thu, 21 Sep 2006 09:21:33 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: sort with Perl ..
Message-Id: <slrneh57vd.k3i.tadmc@magna.augustmail.com>

Taher <tfurnitu@cisco.com> wrote:
> 
> We have data in which each line has several fields , each field
> seperated by tab( or anything else )  .
> We want to to sort these lines of data using say the 3rd field( or any
> other field )  .

> SAMPLE DATA:
> xxxxx   bbb     jjjj
> bbbbb   cccc    yyyyy
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
> 
> ( to be sorted say on the 3rd field)
> FINAL RESULT SEEKED :
> xxxxx   bbb     jjjj
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
> bbbbb   cccc    yyyyy


---------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my @data = (
   "xxxxx\tbbb\tjjjj",
   "bbbbb\tcccc\tyyyyy",
   "cccccc\tppppp\tkkkk",
   "ddddddd\tllllll\tssssss",
);

my $sep = "\t";
my $fnum = 3;

my @sorted = sort { (split /$sep/, $a)[$fnum-1] 
                    cmp 
                    (split /$sep/, $b)[$fnum-1] 
                  } @data;

print "$_\n" for @sorted;
---------------------------------


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 21 Sep 2006 07:44:16 -0700
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: sort with Perl ..
Message-Id: <1158849856.379342.183390@m73g2000cwd.googlegroups.com>

Taher wrote:
> We have data in which each line has several fields , each field
> seperated by tab( or anything else )  .
> We want to to sort these lines of data using say the 3rd field( or any
> other field )  .
>
> Using the Unix shell sort command it is relatively straightforward as
> there is an option on the delimiter and sort field , but not so in Perl
> .
>
> Anyone want to try solving this in Perl ...
>
>
>
> SAMPLE DATA:
> xxxxx   bbb     jjjj
> bbbbb   cccc    yyyyy
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
>
> ( to be sorted say on the 3rd field)
> FINAL RESULT SEEKED :
> xxxxx   bbb     jjjj
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
> bbbbb   cccc    yyyyy

on the command line:

echo 'xxxxx   bbb     jjjj
bbbbb   cccc    yyyyy
cccccc  ppppp   kkkk
ddddddd llllll  ssssss
' | perl -alne '
    $hash{ $F[2] } = $_
}{
    print $hash{$_} for sort keys %hash
'

or in a file:
#####
use strict;
use warnings;

my %hash = ();
while (my $line = <DATA>) {
    my @tmp = split" ", $line;
    $hash{ $tmp[2] } = $line  if @tmp > 2;
}
foreach my $v (sort keys %hash) {
    print $hash{$v};
}
__DATA__
xxxxx   bbb     jjjj
bbbbb   cccc    yyyyy
cccccc  ppppp   kkkk
ddddddd llllll  ssssss
#####

Xicheng



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

Date: Thu, 21 Sep 2006 17:16:24 +0200
From: Mirco Wahab <peace.is.our.profession@gmx.de>
Subject: Re: sort with Perl ..
Message-Id: <eeuakg$6hs$1@mlucom4.urz.uni-halle.de>

Thus spoke Xicheng Jia (on 2006-09-21 16:44):

> on the command line:
> 
> echo 'xxxxx   bbb     jjjj
> bbbbb   cccc    yyyyy
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
> ' | perl -alne '
>     $hash{ $F[2] } = $_
> }{
>     print $hash{$_} for sort keys %hash
> '

This would require to have unique column #N
elements, otherwise they'd get dropped, afaik ...

mybe we could use an array instead:

 perl -ane 'push @A, [$_,$F[2]] }{ print map $_->[0], sort{$a->[1] cmp $b->[1]}@A' < data.txt

Regards

Mirco




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

Date: 21 Sep 2006 08:39:22 -0700
From: "Xicheng Jia" <xicheng@gmail.com>
Subject: Re: sort with Perl ..
Message-Id: <1158853162.325542.99540@i3g2000cwc.googlegroups.com>

Mirco Wahab wrote:
> Thus spoke Xicheng Jia (on 2006-09-21 16:44):
>
> > on the command line:
> >
> > echo 'xxxxx   bbb     jjjj
> > bbbbb   cccc    yyyyy
> > cccccc  ppppp   kkkk
> > ddddddd llllll  ssssss
> > ' | perl -alne '
> >     $hash{ $F[2] } = $_
> > }{
> >     print $hash{$_} for sort keys %hash
> > '
>
> This would require to have unique column #N
> elements, otherwise they'd get dropped, afaik ...

no, you dont need a unique column #N, all records containing more than
3 columns will be counted/sorted. 

Regards,
Xicheng



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

Date: Thu, 21 Sep 2006 16:08:01 GMT
From: "Mumia W. (reading news)" <paduille.4058.mumia.w@earthlink.net>
Subject: Re: sort with Perl ..
Message-Id: <BNyQg.14545$bM.5257@newsread4.news.pas.earthlink.net>

On 09/21/2006 09:19 AM, David Squire wrote:
> Mumia W. (reading news) wrote:
>> [ snipped ]
> 
> What happened to waiting for the OP to at least make an attempt? If
> we're just doing folks work for them today, here's a moduleless version:
> [...]

Yes, I should've waited for the OP's version of the program. I was just 
excited about find out about Sort::Fields :-)

-- 
paduille.4058.mumia.w@earthlink.net


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

Date: Thu, 21 Sep 2006 09:22:57 -0700
From: Taher <tfurnitu@cisco.com>
Subject: Re: sort with Perl ..
Message-Id: <4512BC61.E5215E8F@cisco.com>

Wow .. that was quick ... :-) .

I was struggling to get this .. was not getting the right mix of using
split with arrays ..

Thanks to all Perl gurus ... ( learnt a lot today :-) )


Taher wrote:

> We have data in which each line has several fields , each field
> seperated by tab( or anything else )  .
> We want to to sort these lines of data using say the 3rd field( or any
> other field )  .
>
> Using the Unix shell sort command it is relatively straightforward as
> there is an option on the delimiter and sort field , but not so in Perl
> .
>
> Anyone want to try solving this in Perl ...
>
> SAMPLE DATA:
> xxxxx   bbb     jjjj
> bbbbb   cccc    yyyyy
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
>
> ( to be sorted say on the 3rd field)
> FINAL RESULT SEEKED :
> xxxxx   bbb     jjjj
> cccccc  ppppp   kkkk
> ddddddd llllll  ssssss
> bbbbb   cccc    yyyyy



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

Date: 21 Sep 2006 09:51:18 -0700
From: "DJ Stunks" <DJStunks@gmail.com>
Subject: Re: sort with Perl ..
Message-Id: <1158857475.288816.34190@k70g2000cwa.googlegroups.com>


Taher wrote:
> Wow .. that was quick ... :-) .
>
> I was struggling to get this .. was not getting the right mix of using
> split with arrays ..
>
> Thanks to all Perl gurus ... ( learnt a lot today :-) )

I have the sinking feeling you "learnt" that you can occasionally get
guys to write code for you at c.l.p.m.

-jp



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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 9749
***************************************


home help back first fref pref prev next nref lref last post