Gossamer Forum
Home : General : Perl Programming :

sort problem ... help

(Page 2 of 2)
> >
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
> >