Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
sorting the spread sheet

Posted by neni (neni), 23 April 2005
Hi! I have a multi column spread sheet and for each entry in column 2 I have sorted them based on the number in column 1. I want a complete script in pearl which will pick the entire row for each entry in column 2 only (some entries in column 2 might appear in column 3 before they are in column 2 and needs to be ignored) with the highest column 1 value and write it to another file. Also, the order needs to be preserved in the output file.

I am giving you the example spread sheet and the expected output.

Thanks in advance

input file
col1          col 2                         col 3
812      CV100616      CV098286
707      CV100616      CV100616
672      CV100616      CV097927
668      CV100616      CV099465
637      CV100616      CV097459
605      CV100616      CV097459
587      CV100616      CV097254
651      CV100617      CV100617
723      CV100626      CV096524
721      CV100626      CV100626
693      CV100626      CV095961

i expect the below result            
812      CV100616      CV098286
651      CV100617      CV100617
723      CV100626      CV096524


Posted by admin (Graham Ellis), 23 April 2005
You don't really need a whole program for that - just one line  

earth-wind-and-fire:~/apr05 grahamellis$ cat input
812 CV100616 CV098286
707 CV100616 CV100616
672 CV100616 CV097927
668 CV100616 CV099465
637 CV100616 CV097459
605 CV100616 CV097459
587 CV100616 CV097254
651 CV100617 CV100617
723 CV100626 CV096524
721 CV100626 CV100626
693 CV100626 CV095961
earth-wind-and-fire:~/apr05 grahamellis$ perl -na -e 'print unless ($already{$F[1]}++);' input
812 CV100616 CV098286
651 CV100617 CV100617
723 CV100626 CV096524
earth-wind-and-fire:~/apr05 grahamellis$

Posted by neni (neni), 23 April 2005
on 04/23/05 at 16:29:58, Graham Ellis wrote:
You don't really need a whole program for that - just one line  

earth-wind-and-fire:~/apr05 grahamellis$ cat input
812 CV100616 CV098286
707 CV100616 CV100616
672 CV100616 CV097927
668 CV100616 CV099465
637 CV100616 CV097459
605 CV100616 CV097459
587 CV100616 CV097254
651 CV100617 CV100617
723 CV100626 CV096524
721 CV100626 CV100626
693 CV100626 CV095961
earth-wind-and-fire:~/apr05 grahamellis$ perl -na -e 'print unless ($already{$F[1]}++);' input
812 CV100616 CV098286
651 CV100617 CV100617
723 CV100626 CV096524
earth-wind-and-fire:~/apr05 grahamellis$


Graham thanks for the reply however i forgot to mention that i am working with active state perl on dos and tried to us you regex; can you corresct the below script

thanks

#!/usr/bin/perl -w
open INPUT, "BA.txt";
@arr=<INPUT>;
close INPUT;
split;
foreach (@arr) {
push(@arr2,$_) unless ($already{$F[1]}++);
}
print"@arr2/t";


Posted by admin (Graham Ellis), 23 April 2005
One of the great beauties of Perl is that it's pretty portable between operating systems.    Here's my answer run (this time) on a machine running ActiveState Perl. The only changes I've made are to replace the cat command with type and the single quotes of Unix / Linux parameter protection with double quotes for Windows - the Perl is identical


C:\DOCUME~1\GRAHAM~1>type input
812 CV100616 CV098286
707 CV100616 CV100616
672 CV100616 CV097927
668 CV100616 CV099465
637 CV100616 CV097459
605 CV100616 CV097459
587 CV100616 CV097254
651 CV100617 CV100617
723 CV100626 CV096524
721 CV100626 CV100626
693 CV100626 CV095961

C:\DOCUME~1\GRAHAM~1>perl -na -e "print unless ($already{$F[1]}++);" input
812 CV100616 CV098286
651 CV100617 CV100617
723 CV100626 CV096524

Here's the exact Perl release I used ...

C:\Documents and Settings\Graham Ellis>perl -v

This is perl, v5.8.6 built for MSWin32-x86-multi-thread
(with 3 registered patches, see perl -V for more detail)

Copyright 1987-2004, Larry Wall

Binary build 811 provided by ActiveState Corp. http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Dec 13 2004 09:52:01


If you prefer to use your (longer are arguably more readable!) code, move the split into the foreach loop and rewrite it as

@F = split;

As presented, you're splitting "thin air" once, then using a list called @F that you haven't created - I created it with the -a command line option.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho