CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sort

 



adp
newbie

Feb 27, 2001, 1:48 PM

Post #1 of 8 (797 views)
sort Can't Post

I need some help. I'm a beginner (as you can tell by my code below).
I'm reading in a file (something like this):
apples
peaches
oranges
grapes
I want to sort this file into a new file. I'm jumping around and making thinks more complicated than necessary (note the use of push?) Can you critic me and point me in the right direction? Please & Thanks!

( $input, $OUTfile) = @ARGV;

@fruit_array = ();


open( INPUT, "+<$input" ) or die "can't open input";
open( OUTfile, ">$OUTfile" ) or die "can't open OUTfile";

while (<INPUT>)
{
push(@fruit_array, $_);

for (sort @fruit_array) { print "$_\n" }
print OUTfile;
}

close(INPUT);
close(OUTfile);




muinatit
Deleted

Feb 27, 2001, 4:18 PM

Post #2 of 8 (794 views)
Re: sort [In reply to] Can't Post


Code
my ($input, $output) = @ARGV; 
my @fruits;

open (INPUT, $input) || die $!;
open (OUTPUT, ">$output" ) || die $!;

while (<INPUT>) {
push(@fruit_array, $_);
}

@fruits = sort @fruits;
print OUTPUT @fruits;

close (INPUT);
close (OUTPUT);

Jeffrey "muinatit" Lamoureux
muinatit@hotpop.com


rGeoffrey
User / Moderator

Feb 27, 2001, 6:47 PM

Post #3 of 8 (791 views)
Re: sort [In reply to] Can't Post

If you just want to make the new file and not play with the @fruit array later, you can simplify to...


Code
my ($input, $output) = @ARGV; 

open (INPUT, $input) or die $!;
open (OUTPUT, ">$output" ) or die $!;

print OUTPUT sort <INPUT>;


close (INPUT);
close (OUTPUT);

You don't need to use push because <INPUT> can be used in list context and you get a list. And I switched the "||" to "or" in the open lines because if you forget the parentheses "||" will cause bad things to happen.

---
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


adp
newbie

Feb 28, 2001, 7:35 AM

Post #4 of 8 (783 views)
Re: sort [In reply to] Can't Post

Thanks!



adp
newbie

Feb 28, 2001, 11:40 AM

Post #5 of 8 (777 views)
Re: sort [In reply to] Can't Post

How about if I wanted to sort on the 3rd field in my input file?
(infile looks something like: [variable length comma delimited)
xxxx, bbb, NNN123, xxxxxxxx
xxxxxx, aaaa, NNN123, xx



rGeoffrey
User / Moderator

Mar 1, 2001, 11:46 AM

Post #6 of 8 (766 views)
Re: sort [In reply to] Can't Post

To sort on a particular field you can switch this line...


Code
print OUTPUT sort <INPUT>;

to this more complicated version...


Code
my $SortField = 3; 
print OUTPUT map { $_->[1] }
sort { $a->[0] cmp $b->[0] }
map { [(split (',', $_))[$SortField], $_] }
<INPUT>;

Be sure that the split is working on the correct divider, I have it set for a comma, but your data looks like there might be a space in there also.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


adp
newbie

Mar 2, 2001, 7:40 AM

Post #7 of 8 (752 views)
Re: sort [In reply to] Can't Post

thank you very much..
could you help a little more & explain whats happening?




rGeoffrey
User / Moderator

Mar 2, 2001, 7:10 PM

Post #8 of 8 (745 views)
Re: sort [In reply to] Can't Post

Consider an input file that looks like this


Code
george,gjetson@spacely.com,www.spacely.com 
fred,fflintstone@slate.com,www.bedrock.org
homer,hsimpson@burns.com,www.donuts.com

Now we will read from bottom to top and assume that $SortField == 2

First we read from the file.
After " <INPUT> " the array looks like...

Code
@array = ( 
'george,gjetson@spacely.com,www.spacely.com',
'fred,fflintstone@slate.com,www.bedrock.org',
'homer,hsimpson@burns.com,www.donuts.com',
);

Then we build an array of arrays where the field we want to sort is in position 0 and the whole string is in position 1.
After " map { [(split (',', $_))[$SortField], $_] } " the array looks like...

Code
@array = ( 
['www.spacely.com', 'george,gjetson@spacely.com,www.spacely.com'],
['www.bedrock.org', 'fred,fflintstone@slate.com,www.bedrock.org'],
['www.donuts.com', 'homer,hsimpson@burns.com,www.donuts.com'],
);

Now we can sort on the first field to get the array of arrays in the right order.
After " sort { $a->[0] cmp $b->[0] } " the array looks like...

Code
@array = ( 
['www.bedrock.org', 'fred,fflintstone@slate.com,www.bedrock.org'],
['www.donuts.com', 'homer,hsimpson@burns.com,www.donuts.com'],
['www.spacely.com', 'george,gjetson@spacely.com,www.spacely.com'],
);

Then we retrieve just the part we care about.
After " map { $_->[1] } " the array looks like...

Code
@array = ( 
'fred,fflintstone@slate.com,www.bedrock.org',
'homer,hsimpson@burns.com,www.donuts.com',
'george,gjetson@spacely.com,www.spacely.com',
);

And that is the array that is printed to OUTPUT.

PS. As I did not use chomp, the newline ("\n") is still at the end of each line and both elements of the arrays. But I left them out of the example to make things cleaner.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives