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:
VERY SIMPLE...

 



mattmatt
Deleted

Aug 9, 2000, 2:03 PM

Post #1 of 8 (1162 views)
VERY SIMPLE... Can't Post

im probably sutpid for not knowing this, but how would i assign just EVEN or just ODD numbers to an array??

like maybe something like
@array = even(0..100);
?? that might be it, i dont know

anyway thanx for the help in advance!

[This message has been edited by mattmatt (edited 08-09-2000).]


Kanji
User / Moderator

Aug 9, 2000, 3:30 PM

Post #2 of 8 (1162 views)
Re: VERY SIMPLE... [In reply to] Can't Post

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@even = map { $_ % 2 ) ? () : $_ } 0..100;
@odd = map { $_ % 2 ) ? $_ : () } 0..100;</pre><HR></BLOCKQUOTE>

... which you could turn into functions ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

sub even {
return map { $_ % 2 ? $_ : () } @_;
}</pre><HR></BLOCKQUOTE>

You could also do ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@even = grep /[02468]$/, 0..10;</pre><HR></BLOCKQUOTE>


mattmatt
Deleted

Aug 9, 2000, 6:46 PM

Post #3 of 8 (1162 views)
Re: VERY SIMPLE... [In reply to] Can't Post

hey thanks! damn i dont know how u know so much about perl... =)


Kanji
User / Moderator

Aug 9, 2000, 7:14 PM

Post #4 of 8 (1162 views)
Re: VERY SIMPLE... [In reply to] Can't Post

Self-defence.


mattmatt
Deleted

Aug 9, 2000, 9:16 PM

Post #5 of 8 (1162 views)
Re: VERY SIMPLE... [In reply to] Can't Post

lol

hey u think u can answer another one of my questions?

how can u sort @array that looks like this:
@array = ([ 60, "bob" ],[ 50, "joe" ],[ 75, "jim" ]);

to make it look like this:
@array = ([ 75, "jim" ],[ 60, "bob" ],[ 50, "joe" ]);

all i want to do is arrange the numbers from greatest to least, but take the names with them... then the next thing i want to do is use a foreach statement to print out the first number and the first name... then the second number and the second name, etc.

maybe something like:
foreach @pair (@array) {
print "$pair[0] \n$pair[1]";
}

but again, im st0opid so i dont know...

oh yeah, i cant use
print "$array[0][1]";
and just print out everything from @array because i dont know how many arrays i am going to have within that one array, it depends on the number of members i have


so is that one is easy as the first one? (probably so, i just dont know anything = )


japhy
Enthusiast

Aug 9, 2000, 9:33 PM

Post #6 of 8 (1162 views)
Re: VERY SIMPLE... [In reply to] Can't Post

Hmm, perhaps an article on sorting multidimensional data structures is required; it seems to be a common question, and perhaps more than just a relative perldoc section or FAQ is required.

To sort an array like the one you gave, do this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@sorted = sort { $a->[0] <=> $b->[0] } @array;
</pre><HR></BLOCKQUOTE>

This sorts the array references in @array by the numerical value of the first element in each array reference.

To iterate through such an array with a for loop, use the following idiom:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


for (@array) {
print "$_->[0] $_->[1]\n";
}
</pre><HR></BLOCKQUOTE>

Since each element of @array is an array reference, then $_ is an array reference, so $_->[0] is the first element of the array reference.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



Kanji
User / Moderator

Aug 10, 2000, 5:57 AM

Post #7 of 8 (1162 views)
Re: VERY SIMPLE... [In reply to] Can't Post

And to get that in descending order like you wanted, you can either reverse the output from sort (as it'll be in ascending order) ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@sorted = reverse
sort { $a->[0] <=> $b->[0] }
@array;</pre><HR></BLOCKQUOTE>

... or reverse the sense of comparison within sort itself ($b <=> $a instead of $a <=> $b) ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@sorted = sort { $b->[0] <=> $a->[0] }
@array;</pre><HR></BLOCKQUOTE>

And I think an article on multi-dimensional sorting would be most groovy, especially if paired with (or pointers to) perldsc.


mattmatt
Deleted

Aug 10, 2000, 8:40 AM

Post #8 of 8 (1162 views)
Re: VERY SIMPLE... [In reply to] Can't Post

there's some smart people on these boards =)

thanks!

 
 


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

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