syntax question

J

JS

When using the syntax:

true ? do command1 : do command2 ;

Is it possible to execute more than one command? e.g

true? {do command1 && command3} : { do command 2 && command 4} ;


Thanks for your help,

JS.
 
A

Andreas Kahari

When using the syntax:

true ? do command1 : do command2 ;

Is it possible to execute more than one command? e.g

true? {do command1 && command3} : { do command 2 && command 4} ;

The ( test ? value1 : value2 ) construct is best suited as
the right hand side of an assignment. If you need "value1"
and/or "value2" to do more elaborate things than just provide
a straight value, then consider using "if (test) {value1} else
{value2}" instead (where the values now are chunks of code).
 
S

Sam Holden

When using the syntax:

true ? do command1 : do command2 ;

Is it possible to execute more than one command? e.g

true? {do command1 && command3} : { do command 2 && command 4} ;

You can use any expression, "command" is a meaningless term in
that context.

You can use operators like , to turn multiple expressions into
one expressions, and functions or do to turn arbitrary
statments into an expression.

Though using if..else will usually make for more readable
code, if the expressions are large.
 
J

JS

Sam said:
You can use any expression, "command" is a meaningless term in
that context.

You can use operators like , to turn multiple expressions into
one expressions, and functions or do to turn arbitrary
statments into an expression.

Though using if..else will usually make for more readable
code, if the expressions are large.

Here is the code I'm trying to get to work:

my $firstdept=1;
foreach my $app (keys %appdept_percentage){
my $applen=length($app)+4;
$firstdept?$firstdept=0,printf "\n%35s",$app:printf
"%".$applen."s",$app
}

Admittedly I could write it:

my $firstdept=1;
foreach my $app (keys %appdept_percentage){
my $applen=length($app)+4;
$firstdept?printf "\n%35s",$app:printf "%".$applen."s",$app
$firstdept=0;
}

OR

my $firstdept=1;
foreach my $app (keys %appdept_percentage){
my $applen=length($app)+4;
if (){
printf "\n%35s",$app
$firstdept=0;
}
else{ printf "%".$applen."s",$app
}
}

OR probably a dozen other ways, but my simple question is assuming I
could do this, what is the correct syntax to group this

$firstdept=0,printf "\n%35s",$app

between the ? and :

This is syntactically wrong at the moment:

$firstdept?$firstdept=0,printf "\n%35s",$app:printf "%".$applen."s",$app

This won't work:
$firstdept?$firstdept=0 && printf "\n%35s",$app:printf "%".$applen."s",$app

Nor will this:

$firstdept?($firstdept=0,printf "\n%35s",$app):printf "%".$applen."s",$app

or this:

$firstdept?{$firstdept=0,printf "\n%35s",$app}:printf "%".$applen."s",$app

I don't care about legibility, I just want to know if Perl can do it or
not?!!!!!!

Thanks,

JS.
 
A

Anno Siegel

JS said:
Here is the code I'm trying to get to work:

my $firstdept=1;
foreach my $app (keys %appdept_percentage){
my $applen=length($app)+4;
$firstdept?$firstdept=0,printf "\n%35s",$app:printf
"%".$applen."s",$app
}

Admittedly I could write it:

my $firstdept=1;
foreach my $app (keys %appdept_percentage){
my $applen=length($app)+4;
$firstdept?printf "\n%35s",$app:printf "%".$applen."s",$app
$firstdept=0;
}

Then why don't you? Well, it would make a difference if $firstdept could
meaningfully be boolean false, but not 0 ("" or undef).
OR

my $firstdept=1;
foreach my $app (keys %appdept_percentage){
my $applen=length($app)+4;
if (){
printf "\n%35s",$app
$firstdept=0;
}
else{ printf "%".$applen."s",$app
}
}

Did you try it? An empty "if"-condition is a syntax error.
OR probably a dozen other ways, but my simple question is assuming I
could do this, what is the correct syntax to group this

$firstdept=0,printf "\n%35s",$app

between the ? and :

This is syntactically wrong at the moment:

$firstdept?$firstdept=0,printf "\n%35s",$app:printf "%".$applen."s",$app

Sure. There can be only one expression between "?" and ":".
This won't work:
$firstdept?$firstdept=0 && printf "\n%35s",$app:printf "%".$applen."s",$app

Wrong precedence. "&&" binds tighter than "=", so that's
"$firstdept= ( 0 && printf "\n%35s",$app)". Not what you want.
Nor will this:

$firstdept?($firstdept=0,printf "\n%35s",$app):printf "%".$applen."s",$app

Sure it will. How did you test it? The parens make a single expression
out of the comma-separated two.
or this:

$firstdept?{$firstdept=0,printf "\n%35s",$app}:printf "%".$applen."s",$app

A block isn't a statement, so a block can't go between "?" and ":". To
make a statement out of a block, prefix it with "do". IoW:

$firstdept? do {$firstdept=0,printf "\n%35s",$app}:printf "%".$applen."s",$app
I don't care about legibility, I just want to know if Perl can do it or
not?!!!!!!

If you had run your tests a little more careful, you'd already have your
answer. Don't blame the group.

Anno
 
J

JS

Anno said:
Then why don't you? Well, it would make a difference if $firstdept could
meaningfully be boolean false, but not 0 ("" or undef).




Did you try it? An empty "if"-condition is a syntax error.




Sure. There can be only one expression between "?" and ":".




Wrong precedence. "&&" binds tighter than "=", so that's
"$firstdept= ( 0 && printf "\n%35s",$app)". Not what you want.




Sure it will. How did you test it? The parens make a single expression
out of the comma-separated two.




A block isn't a statement, so a block can't go between "?" and ":". To
make a statement out of a block, prefix it with "do". IoW:

$firstdept? do {$firstdept=0,printf "\n%35s",$app}:printf "%".$applen."s",$app




If you had run your tests a little more careful, you'd already have your
answer. Don't blame the group.

Anno

Thanks for that Anno. So you can do it with a do {block}. I don't mean
to blame the "group" but if someone had just said that in the first
place it would have been a lot easier :)

JS.
 
T

Tore Aursand

When using the syntax:

true ? do command1 : do command2 ;

Is it possible to execute more than one command? e.g

true? {do command1 && command3} : { do command 2 && command 4} ;

Have you tried? What happened when you did?
 
S

Sam Holden

Anno Siegel wrote: [some stuff which I trimmed]
Thanks for that Anno. So you can do it with a do {block}. I don't mean
to blame the "group" but if someone had just said that in the first
place it would have been a lot easier :)

I did.
 
E

Eric Bohlman

JS said:
Here is the code I'm trying to get to work:

my $firstdept=1;
foreach my $app (keys %appdept_percentage){
my $applen=length($app)+4;
$firstdept?$firstdept=0,printf "\n%35s",$app:printf
"%".$applen."s",$app
}

The first thing to note is that setting $firstdept to zero can be done
unconditionally, since it should be zero after you've printed any line.

The second thing to note is that once you've removed the above assignment,
you're left with two printf's that differ only in their format strings.
Therefore, you should have one printf with a conditional expression to
determine the appropriate format string. I'd write it as:

my $firstdept=1;
foreach my $app (keys %appdept_percentage){
my $applen=length($app)+4;
printf $firstdept?"\n%35s":"%${applen}s",$app;
$firstdept=0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top