Gossamer Forum
Home : General : Perl Programming :

sort problem ... help

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
Quote Reply
Re: [yogi] sort problem ... help In reply to
also, with

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

print @catl;

@catl is not sorted. please try with your file.
Quote Reply
Re: [robyone] sort problem ... help In reply to
No.

I have exactly the code as I gave you above, and I use as the input file
Code:
yogi This street Moon
robyone that street somewhere
paul some other street England


and the output is

Code:
paul
robyone
yogi

Can't help you more than that. And print @catl doesn't really make sense.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] sort problem ... help In reply to
Just to be safe (incase you have differing cases) you should use:

Code:
my @sorted = sort { lc($a->[0]) cmp lc($b->[0]) } @unsorted;
Quote Reply
Re: [robyone] sort problem ... help In reply to
mmmhhh. believe me...

for (@catl) { print "$_->[1] <br>"; }

for me produces only the last value/line (foreach line in my db).

i still need help :((

could it be related to use strict; ?

Last edited by:

robyone: Apr 22, 2002, 6:18 AM
Quote Reply
Re: [robyone] sort problem ... help In reply to
Well yeah thats what its meant to do. Yogi was only giving you an example.

$_->[x] prints each element so just add the extra elements you want to print.

If you are looking for someone to write all the code then maybe you should go to the custom jobs forum.

Last edited by:

Paul: Apr 22, 2002, 6:18 AM
Quote Reply
Re: [Paul] sort problem ... help In reply to
Paul,

the problem is that it prints only the last line in my db. It multiplies -forearch record in the db- the last record found .... Resulting that $_->[0] produces

paul
paul
paul


from a text file like:
-----
roby
yogy
paul
-----
Quote Reply
Re: [robyone] sort problem ... help In reply to
Can you post your code, then we might see what is wrong.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [robyone] sort problem ... help In reply to
Try:

Code:
print join(" ", @$_) for (@catl);

What does that print?

Edit: Yeah do as Yogi said :)

Last edited by:

Paul: Apr 22, 2002, 6:37 AM
Quote Reply
Re: [Paul] sort problem ... help In reply to
Great guys you helped me solve this.
Thank you for your help.

Last edited by:

robyone: Apr 22, 2002, 6:49 AM
Quote Reply
Re: [Paul] sort problem ... help In reply to
Hi

how can i print the entire record

for (@catl) { print "$_"; } # will this work???
Quote Reply
Re: [robyone] sort problem ... help In reply to
Huh?

Something like;

foreach (@catl) { print $_; }

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy.] sort problem ... help In reply to
it is not working ... it is printed as
ARRAY(0x1a45084)ARRAY(0x1afac3c)

also i can't do

if ($_ =~ /$keyword/i) {
Quote Reply
Re: [robyone] sort problem ... help In reply to
Code:
print join ("<BR>", map { @$_ } @catl);

Last edited by:

Paul: May 15, 2002, 6:41 AM
Quote Reply
Re: [Paul] sort problem ... help In reply to
how can i do this

if ($_ =~ /$keyword/i) {
Quote Reply
Re: [robyone] sort problem ... help In reply to
Basically just like you have it except I'd use:

Code:
if (/^\Q$keyword\E$/i) {

Last edited by:

Paul: May 15, 2002, 6:55 AM
Quote Reply
Re: [Paul] sort problem ... help In reply to
not working at all :(((

$_ is ARRAY(0x1a44e6c) or similar

how can i create a working if
Quote Reply
Re: [robyone] sort problem ... help In reply to
I don't understand what you mean?

Are you trying to match something in one of the arrayrefs?....I thought you were just asking in general

What code do you have so far?
Quote Reply
Re: [robyone] sort problem ... help In reply to
Do you mean like:

Code:
for my $array (@catl) {
if (grep { /^\Q$something\E$/i } @$array) {
print "Found $something\n";
}
}
Quote Reply
Re: [Paul] sort problem ... help In reply to
here it is. i can't match $_ ( ARRAY(xxxxxxx) )against $keywork. thanks!!

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

@catl = sort { lc($a->[1]) cmp lc($b->[1]);

for (@catl){

if ($_ =~ /$keyword/i) {

....
Quote Reply
Re: [Paul] sort problem ... help In reply to
not working for me :(((
Quote Reply
Re: [robyone] sort problem ... help In reply to
Works ok for me...

Code:
#!/usr/bin/perl

print "Content-type: text/plain\n\n";

my @array = ( [1,2,3], [4,5,6], [7,8,9] );

for (@array) {
if (grep { /^5$/ } @$_) {
my $i = grep { /^5$/ } @$_;
print "Found: 5 - Element $i\n";
}
else {
print "Skipping...\n";
}
}

Last edited by:

Paul: May 15, 2002, 8:46 AM
Quote Reply
Re: [Paul] sort problem ... help In reply to
will this look on the entire string $_ for $keyword ?
Quote Reply
Re: [robyone] sort problem ... help In reply to
$_ is a array ref,
you should de-ref it. like what Paul say, @$_;
you probably want to match one by one in the array.
then you can do like
@$_[indexnumber] =~ /keyword/
Quote Reply
Re: [robyone] sort problem ... help In reply to
$_ is an array ref, @$_ is what you need - it will look for $keyword in each element of the array reference.
Quote Reply
Re: [Paul] sort problem ... help In reply to
if (grep { /^$KEYWORD$/ } @$_)

is this ok?

Last edited by:

robyone: May 15, 2002, 9:12 AM