Gossamer Forum
Home : General : Perl Programming :

sort problem ... help

(Page 1 of 2)
> >
Quote Reply
sort problem ... help
foreach $cln (@catl){
@data=split(/\t/,$cln);


... how can i sort with something like
sort $data[2]{$b} <=> $data[2]{$a};

what's the right code. shall i loop with another foreach? please help?
Quote Reply
Re: [robyone] sort problem ... help In reply to
Have you looked at the examples on the following page?

http://www.perldoc.com/...1/pod/func/sort.html

- wil
Quote Reply
Re: [Wil] sort problem ... help In reply to
it does not help much ... i've already seen that page :(
Quote Reply
Re: [robyone] sort problem ... help In reply to
You have your syntax wrong...the page Wil provided shows the correct syntax.
Quote Reply
Re: [Paul] sort problem ... help In reply to
can i do it in the same foreach?
Quote Reply
Re: [robyone] sort problem ... help In reply to
Well yeah if you want. It is the actual sort code that is incorrect. There are many examples on the perldoc page.

Notice how your sort codes are missing { } and aren't specifying what to sort.

Last edited by:

Paul: Apr 22, 2002, 3:10 AM
Quote Reply
Re: [Paul] sort problem ... help In reply to
here it is. this is what i need to do. sort @catl by $data[5]. can you fix this code?


foreach $cln (@catl){
@data=split(/\t/,$cln);
sort $data[5]{$b} <=> $data[5]{$a};
if ($data[3] =~ /$fldvalue/i){
push (@sresults,"$cln");
}
}


thank you.

Last edited by:

robyone: Apr 22, 2002, 3:11 AM
Quote Reply
Re: [robyone] sort problem ... help In reply to
Ok Im a little bit unsure about what you mean. $data[5] is only one element. How and what are you trying to sort?
Quote Reply
Re: [Paul] sort problem ... help In reply to
a flat file including addresses:

-----------------------------
name [tab] address [tab] city ... etc.
name [tab] address [tab] city ... etc.
name [tab] address [tab] city ... etc.
name [tab] address [tab] city ... etc.
-----------------------------
Quote Reply
Re: [robyone] sort problem ... help In reply to
http://www.perlfaq.com/.../view?view_by_id=203

- wil
Quote Reply
Re: [robyone] sort problem ... help In reply to
Yes but how are you trying to sort them?
Quote Reply
Re: [Wil] sort problem ... help In reply to
can i use $data[5] insted of last_name? or ...should i need to define last_name. i would need to use $data[x]

thanks
Quote Reply
Re: [robyone] sort problem ... help In reply to
open (CTLG,$filename); @fcatl=<CTLG>; close(CTLG);

foreach $fcln (@fcatl){
@fdata=split(/\t/,$fcln);
}

@catl= sort { $a->{$fdata[0]} <=> $b->{$fdata[0]}} @fcatl;


why @catl is not working/sorting based on $fdata[0] ? :((
Quote Reply
Re: [robyone] sort problem ... help In reply to
Try:

Code:
open (CTLG,$filename);
@fcatl=<CTLG>;
close(CTLG);

my @unsorted;

foreach $fcln (@fcatl){
@fdata = split(/\t/,$fcln);
push @unsorted, \@fdata;
}

my @sorted = sort {$a->[3] <=> $b->[3]} @unsorted;
@sorted is then an array of arrayrefs, and you would access the fields as $sorted->[2].

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] sort problem ... help In reply to
my @unsorted;

foreach $fcln (@fcatl){
@fdata=split(/\t/,$fcln);
push (@unsorted,@fdata);

}

@catl = sort {$a->[1] <=> $b->[1]} @unsorted;

print @catl ; #use this to check sorting

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

not sorting correctly!


how can i 'print' fields once ordered. How can i get field positions --> $data[position] IN THE NEW ARRAY.

Thanks,

Last edited by:

robyone: Apr 22, 2002, 4:59 AM
Quote Reply
Re: [robyone] sort problem ... help In reply to
Field positions: $sorted->[position]

Print: depends on what you want to print.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] sort problem ... help In reply to
se above ... it is not ordering correctly.

what i want to print later ... is:


foreach $cln (@catl){

if ($WHATFIELDPOSITION? =~ /$fldvalue/i){
$searchresultsrow.="<li>$cln";
}
}

Last edited by:

robyone: Apr 22, 2002, 5:06 AM
Quote Reply
Re: [robyone] sort problem ... help In reply to
your push command is wrong, i.e. you don't want to put in an array but an arrayref.

push @unsorted, \@fdata;

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] sort problem ... help In reply to
push @unsorted, \@fdata;

produces:

ARRAY(0xcc5f60)ARRAY(0xcc5f60)ARRAY(0xcc5f60)ARRAY(0xcc5f60)ARRAY(0xcc5f60)ARRAY(0xcc5f60)ARRAY(0xcc5f60)ARRAY(0xcc5f60)ARRAY(0xcc5f60 ... etc.
Quote Reply
Re: [robyone] sort problem ... help In reply to
That's what it is supposed to do.

You can access the fields as $sorted[1]->[2].

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] sort problem ... help In reply to
please try a complete process based on what you suggested. it is not working. :(
Quote Reply
Re: [robyone] sort problem ... help In reply to
sorry my mistake.

You should use cmp instead of <=> for string values.

Code:
#!/usr/bin/perl

use strict;

my $filename = 'test.txt';

open (CTLG,$filename);
my @fcatl=<CTLG>;
close(CTLG);

my @unsorted;

foreach my $fcln (@fcatl){
my @fdata = split(/\t/,$fcln);
push @unsorted, \@fdata;
}

my @sorted = sort {$a->[0] cmp $b->[0]} @unsorted;

for (@sorted) {
print "$_->[1] \n";
}

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] sort problem ... help In reply to
how can you solve that

for (@sorted) { print "$_->[1] \n"; }

looks only the last record/line of my db

also, how can i search in a specific field position... once i can read the entire 'sorted' db

thanks.
Quote Reply
Re: [robyone] sort problem ... help In reply to
I don't understand your questions.

Can you clarify please?

BTW, I just used arbitrary numbers for the sorting (i.e. you might have to change [1] or so to the field you want).

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] sort problem ... help In reply to
with -for example- a file with this format
----------------
name1 address1 city1
name2 address2 city2
name3 address3 city3
name4 address4 city4
----------------

$_->[0] will print:

name4
name4
name4
name4
> >