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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
{} inside if (whatever) { doWhatever }



Jun 25, 2000, 3:48 AM

Post #1 of 5 (2843 views)
{} inside if (whatever) { doWhatever } Can't Post

What should I use instead of if (something) { whatever } if 'whatever' contains {}?

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

if ($r eq "true") { my @rows = reverse sort { $a->[$column] <=> $b->[$column] } map { chomp; [split(/:::/)] } DATA>; }

else { my @rows = sort { $a->[$column] cmp $b->[$column] } map { chomp; [split(/:::/)] } <DATA>; }</pre><HR></BLOCKQUOTE>
this doesn't work

User / Moderator

Jun 24, 2000, 10:46 PM

Post #2 of 5 (2843 views)
Re: {} inside if (whatever) { doWhatever } [In reply to] Can't Post

 <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">quote:</font><HR>Your (kanji) second code didn't work, don't know why, don't care<HR></BLOCKQUOTE>

Ah, but you should care: you'll learn more and know better next time. Smile

I just gave an abbreviated example, so it wouldn't of worked w/o the $a and $b's being swapped out for their longer $var->[$column] versions.

User / Moderator

Jun 25, 2000, 5:17 AM

Post #3 of 5 (2843 views)
Re: {} inside if (whatever) { doWhatever } [In reply to] Can't Post

Typo aside, I see no reason why it wouldn't work, so please define "doesn't work".

The only thing I can think of is that you're trying to access @rows from outside the if/else block, when (with my) you're limiting that array to only the block.

If that's the case, you want to define @rows beforehand ...

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

my @rows;
if ( ... } { @rows = ... }
else { @rows = ... }</pre><HR></BLOCKQUOTE>

To save repetitive coding, however, you should consider something like ...

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

my $filter = ( $r eq 'true' ) ?
sub { $b <=> $a } : # reverse numerically
sub { $a cmp $b }; # ASCIIbetically

my @rows = sort $filter map { ... } <DATA>;</pre><HR></BLOCKQUOTE>


Jun 25, 2000, 5:23 AM

Post #4 of 5 (2843 views)
Re: {} inside if (whatever) { doWhatever } [In reply to] Can't Post

It's hard to see what it's supposed to be doing. If you run into a problem with the code, you might want to organize it across multiple lines and to avoid the use of $_ so that everything is clear.

One thing that doesn't make sense is:

map { chomp; [split(/:::/)] } DATA>;

I'm not that familiar with map but shouldn't it be using () and not {}?

Oh wait, I see. The {} is a routine being applied to <DATA>. Your missing a <. I didn't even know what DATA> was and was about to comment on that too.

Btw, here are a less repetative alternative:

my @rows = sort
{ $a->[$column] <=> $b->[$column] }
map { chomp; [split(/:::/)] } <DATA>;

if ($r eq "true")
{ @rows = reverse @rows; }

This would be my preference. It's, IMO, easier to read and, by only having the sort once, easier to modify.


Jun 25, 2000, 8:09 AM

Post #5 of 5 (2843 views)
Re: {} inside if (whatever) { doWhatever } [In reply to] Can't Post

Thanks to both of you.

Why it didn't work was probably because of the my (at least it worked directly when I remade it).

Your (kanji) second code didn't work, don't know why, don't care because I got it to work with that simple $b <=> $a thing. I actually tried this once before but it didn't work, the reason for this is that I hd the date as month/day in the data file and the dash didn't work with <=> so it always used cmp.
And DrZed, that typo was there due to the fact I had to remove some line changes because of bad wrapping when using copy & paste.

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

my @rows = sort { $b->[$column] <=> $a->[$column] | | $a->[$column] cmp $b->[$column] }
map { chomp; [split(/:::/)] } <DATA>;</pre><HR></BLOCKQUOTE>

and you'll find the complete script here:

Thanks again for helping me with this (now hopefully complete) script Smile

[This message has been edited by mwaf (edited 06-25-2000).]


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

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