Ternary Conditional Operator Question 
Author Message
 Ternary Conditional Operator Question

Ok folks.

I'm usually pretty good at figuring this stuff out, but this one is

I'm trying to see if $slash =~ m{/} is a match, and then I want to do
a substitution on $_ depending on the outcome of the match.  I've
tried a million ways to parenthesize, but no luck.

Could someone out there please let me know why this doesn't work:


    # $slash == '';


        $slash =~ m{/} ? s{\\}{/}g : s{/}{\\}g;

    # This doesn't work either...
    #   ($slash =~ m{/}) ? s{\\}{/}g : s{/}{\\}g;
    }

I'm hoping this is going to be a "duh" moment for me.

Thanks,

Jonathan



Sun, 04 Jan 2004 04:55:28 GMT  
 Ternary Conditional Operator Question

Quote:

> Ok folks.

> I'm usually pretty good at figuring this stuff out, but this one is

> I'm trying to see if $slash =~ m{/} is a match, and then I want to do
> a substitution on $_ depending on the outcome of the match.  I've
> tried a million ways to parenthesize, but no luck.

> Could someone out there please let me know why this doesn't work:


>     # $slash == '';

You've set the value of $slash to the null string so it will never match
m{/}.

Quote:

>         $slash =~ m{/} ? s{\\}{/}g : s{/}{\\}g;

This doesn't make much sense. If the string has a slash in it then
change all back-slashes to slashes. However, if the string has _no_
slashes in it then change the slashes (which aren't there) to
back-slashes.

Quote:
>     # This doesn't work either...
>     #   ($slash =~ m{/}) ? s{\\}{/}g : s{/}{\\}g;
>     }

> I'm hoping this is going to be a "duh" moment for me.

You think?

John
--
use Perl;
program
fulfillment



Sun, 04 Jan 2004 06:49:09 GMT  
 Ternary Conditional Operator Question

Quote:
> Ok folks.

> I'm usually pretty good at figuring this stuff out, but this one is

> I'm trying to see if $slash =~ m{/} is a match, and then I want to
> do a substitution on $_ depending on the outcome of the match.  I've
> tried a million ways to parenthesize, but no luck.

> Could someone out there please let me know why this doesn't work:


>     # $slash == '';


>         $slash =~ m{/} ? s{\\}{/}g : s{/}{\\}g;

>     # This doesn't work either...
>     #   ($slash =~ m{/}) ? s{\\}{/}g : s{/}{\\}g;
>     }

> I'm hoping this is going to be a "duh" moment for me.

Seems to work fine for me -- perhaps the problem is actually somewhere
else?  You didn't actually say in what way this failed.  Can you give
a complete script that demonstrates the problem?

--
Ren Maddox



Sun, 04 Jan 2004 05:19:59 GMT  
 Ternary Conditional Operator Question

Quote:

> Ok folks.

> I'm usually pretty good at figuring this stuff out, but this one is

> I'm trying to see if $slash =~ m{/} is a match, and then I want to do
> a substitution on $_ depending on the outcome of the match.  I've
> tried a million ways to parenthesize, but no luck.

> Could someone out there please let me know why this doesn't work:


>     # $slash == '';


>         $slash =~ m{/} ? s{\\}{/}g : s{/}{\\}g;

>     # This doesn't work either...
>     #   ($slash =~ m{/}) ? s{\\}{/}g : s{/}{\\}g;
>     }

> I'm hoping this is going to be a "duh" moment for me.



        # either of these will do - there is no need for a ternary op

        s{/}{\\}g;
#       s{/}{\\}g if m{/};

Quote:
}

--
  ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852

  / ) /--<  o // //      http://dbecoll.webjump.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/


Sun, 04 Jan 2004 09:07:44 GMT  
 Ternary Conditional Operator Question

Quote:
> Ok folks.

> I'm usually pretty good at figuring this stuff out, but this one is

> I'm trying to see if $slash =~ m{/} is a match, and then I want to do
> a substitution on $_ depending on the outcome of the match.  I've
> tried a million ways to parenthesize, but no luck.

> Could someone out there please let me know why this doesn't work:


>     # $slash == '';


>         $slash =~ m{/} ? s{\\}{/}g : s{/}{\\}g;

>     # This doesn't work either...
>     #   ($slash =~ m{/}) ? s{\\}{/}g : s{/}{\\}g;
>     }

I'll jump in and ask 'are you trying to solve the wrong problem?'

What are you trying to achieve?

You do realise that forward slashes are quite acceptable in a Perl program
on a Win32 box?

Wyzelli
--
($a,$b,$w,$t)=(' bottle',' of beer',' on the wall','Take one down, pass it
around');
for(reverse(1..100)){$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n$_$a$s$b\n$t\n";
$_--;$s=($_!=1)?'s':'';$c.="$_$a$s$b$w\n\n";}print"$c*hic*";



Sun, 04 Jan 2004 09:29:39 GMT  
 Ternary Conditional Operator Question

Quote:

> > Could someone out there please let me know why this doesn't work:


> >     # $slash == '';


> >         $slash =~ m{/} ? s{\\}{/}g : s{/}{\\}g;

> >     # This doesn't work either...
> >     #   ($slash =~ m{/}) ? s{\\}{/}g : s{/}{\\}g;
> >     }

> I'll jump in and ask 'are you trying to solve the wrong problem?'

> What are you trying to achieve?

> You do realise that forward slashes are quite acceptable in a Perl program
> on a Win32 box?

> Wyzelli

Sorry for being so vague, but I expected it not to work for you the
same way it didn't work for me.  :)  First of all, I'm trying to
change all backslashes to forwards and likewise because I'm writing
some code for a poorly planned system that sometimes requires forward
slashes and sometimes not (lame).

When I run the above code I get an "unable to modify [something
something]" with -w (without -w the code just stopped running with no
complaints).


(which happed to be the params passed to my sub.  I fixed it by

it wouldn't work tho'.

Is that better?

Thanks,

JC

Here's the whole thing:

package TeamSite::Utils;

use strict;

sub new {
    my $self = {};
    bless $self;
    return $self;

Quote:
}

sub slashFix {
#       Takes an array of strings or an array of variables as it's
#       first argument(s) and an optional slash (/) as its last.  
#       If the last argument is not a slash, all of the previoius
#       arguments will have their slashes changed to backslashes(\).
#       If the last argument is a slash, all previous arguments will
#       have their backslashes changed to slashes.  Returns an array
#       of processed arguments if called in list context, and
#       returns a string if called in string context.
    my $self = shift;




        ($slash =~ m{/}) ? s{\\}{/}g : s{/}{\\}g;

$_);
    }


Quote:
}

1; # True-Value Hardcode


Mon, 05 Jan 2004 06:40:05 GMT  
 Ternary Conditional Operator Question

Quote:

> You do realise that forward slashes are quite acceptable in a Perl program
> on a Win32 box?

Not everywhere - shelling out can be a problem.

--
  ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852

  / ) /--<  o // //      http://dbecoll.webjump.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/



Mon, 05 Jan 2004 07:14:31 GMT  
 Ternary Conditional Operator Question

Quote:


>> > Could someone out there please let me know why this doesn't work:

[ snip code that *does* work ]

Quote:
>> What are you trying to achieve?
>Sorry for being so vague, but I expected it not to work for you the

                               ^^^^^^^^^^

Just write it in the form of a short and complete program, then
run the program, _then_ post that code.

Then we will be troubleshooting the same code that you have,
instead of wasting time on code that does not even really exist.

Quote:
>When I run the above code I get an "unable to modify [something
>something]" with -w (without -w the code just stopped running with no
>complaints).

You should include the exact message text if you want help diagnosing
the problem.

Quote:

>(which happed to be the params passed to my sub.  I fixed it by

>it wouldn't work tho'.

Because function args are aliases.

Quote:
>Is that better?

No. It does not execute either.

We cannot see the method call. I'll bet the args in the call
are not lvalues, but I can't really tell because I am troubleshooting
invisible code...

Quote:
>Here's the whole thing:

>package TeamSite::Utils;
>sub slashFix {
>    my $self = shift;



                                       ^^^^^^

Perl has operators for testing equality. You should use one of
them when you want to test for equality:


                              ^^^^^^^^^^^

The reason would be revealed if we could see the method call.

But we can't. So we cannot help with your real problem...

Quote:

>        ($slash =~ m{/}) ? s{\\}{/}g : s{/}{\\}g;

                            ^^^^^^^^^   ^^^^^^^^^


the caller. That would be OK with:


but would complain with:

   $util->slashFix( qw/zero one two three/ );

because the are not lvalues in the second case.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Mon, 05 Jan 2004 07:31:10 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. ternary conditional operator

2. A question on the Conditional Operator

3. newbie questions re conditional operators

4. problem with ternary operator

5. ternary operator

6. Conditional Operator behavior?

7. Three Operand Conditional Operator

8. push onto trinary conditional operator?

9. Q: conditional operator and variable assignment

10. conditional operator "?:"

11. problem parsing conditional operator.

12. conditional replacement (newbie question)

 

 
Powered by phpBB® Forum Software