Switch statement control.

Switch statement control.

00Six00
Contributor Contributor
1,260 Views
4 Replies
Message 1 of 5

Switch statement control.

00Six00
Contributor
Contributor

Hi everyone,

Ive been trying to figure this one out for a few days. I have 6 things that i need to be able to toggle between but i need the switch statement to always begin at "case 0".  Is it possible to make a switch statement always begin at "case 0" when I execute the command and if so, how can i accomplish this? 

0 Likes
1,261 Views
4 Replies
Replies (4)
Message 2 of 5

mcw0
Advisor
Advisor

Not clear what you mean by "always start at case 0".  Can you be more specific?

0 Likes
Message 3 of 5

00Six00
Contributor
Contributor

this is a base example as ive only been playing around with this for the past couple of days. If i needed case 3 on 1 object, i would need to toggle 4 times. Then say i needed case 0 on a different object, i would need to toggle and go through the rest of the switch statement before it reaches case 0 again. I was just wondering if there was a way that once ive reached the desired case, if the switch statement can be reset so that it begins a case 0 once i need it again. 

global proc example()
{
    global int $Toggle;
    string $selections[] = `ls -sl`;
    $Toggle++;
    switch ($Toggle)
    {
        case 0:
                for ($each in $selections)
                {
                    do something;
                }
                break;
        case 1:
                for ($each in $selections)
                {
                    do something;
                }
                break;
        case 2:
                for ($each in $selections)
                {
                    do something;
                }
                break;
        case 3:
                for ($each in $selections)
                {
                    do something;
                }
                break;
        case 4:
                for ($each in $selections)
                {
                    do something;
                }
                break;
        case 5:
                for ($each in $selections)                
                {
                    do something;
                }
                break;
        default:
                $Toggle = 0;
                for ($each in $selections)                
                {
                    do something;
                }
                break;
    }
    
    select -r $selections;
    
}

example();

 

0 Likes
Message 4 of 5

mcw0
Advisor
Advisor

Why use "toggle"? Don't you know what you want to do with your selection?  Why not just run the command you want on your selection? 

 

I'm sorry that I am failing to see what you are trying to do.  Also, print statements are a great aid in working through your code. 

0 Likes
Message 5 of 5

bradley_henke
Enthusiast
Enthusiast

It your default case you are setting the value to 0, but you immediately increment the value to 1 with the statement "$Toggle++" before the switch statement. I would either set the value to -1 in your default branch, or move the "$Toggle++" after the switch statement.

 

 

0 Likes