in reply to Re: Case Statement
in thread Case Statement

switch ($array[0]) { case (/Time_Stamp/i) { @time = split (/-/, $array[1]); $date_submited = $time[0]; $time_submited = $time[1]; } case (/eqpid/i) { $eqpid = $array[1]; # $eqpid=substr ($array[1], -1, 1, ); $chambernum = substr ($array[1], -1, 1); switch ($chambernum){ case (/A/i) {$chamber = 1;} case (/B/i) {$chamber = 2;} case (/C/i) {$chamber = 3;} case (/D/i) {$chamber = 4;} } } case (/AlarmType/i) {$alarmtype = "$array[1]";} case (/LotID/i) { @lotid = split( /-/, $array[1] ); $lotid = "$lotid[0]"; $wafer_flow = "$lotid[1]-$lotid[2]"; } case (/Code/i) {$code = "$array[1]";} case (/UserCode/i) { # $usercode = "$array[1]"; print "User = $array[1]"; } look at the case for code and Usercode

Replies are listed 'Best First'.
Re^3: Case Statement
by Joost (Canon) on May 25, 2007 at 20:33 UTC
      thank you so much i try that and it work. Also for my LotID, when i print out it come out as (Lot#1 . How do i remove the ( from Lot#1? I'm not sure how to use the regex command here thank again
        It depends on what you want exactly, but here's something that might work for you:
        case (/LotID/i) { @lotid = split( /-/, $array[1] ); $lotid = "$lotid[0]"; # fetch the part matching Lot#XXX where XXX is one or + more digits, or die if that doesn't work ($lotid) = $lotid =~ /(Lot#\d+)/ or die "lotid isn't c +orrectly formatted"; $wafer_flow = "$lotid[1]-$lotid[2]"; }
        The ($lotid) = ... line makes use of several tricks:

        1. assigning the result of a regex match to a list will assing the () delimited parts of that regex to the the members of the list. In this case, there's only one member in the list ($lotid) and one () delimited submatch, so $lotid will get the value of whatever is matched by Lot#\d+

        2. the result of assignment to a list is the number of elements assigned to. in this case, 1 if the regex matches or 0 if the regex doesn't match. The ... or die ... part tests for that value and throws an exception if the match failed, since in that case my logic is wrong.

        For a reasonably genteel introduction to regexes see perlretut. For (most of) the whole story, see perlre.

        As an aside, please consider dropping Switch altogether, it's a fun module but one day it will cause hard to debug problems.

Re^3: Case Statement
by girarde (Hermit) on May 25, 2007 at 20:31 UTC
    I'd be more inclined to use a hash whose keys are the appropriate case values, some of which would be regexes to deal with Code/UserCode.
Re^3: Case Statement
by roboticus (Chancellor) on May 26, 2007 at 13:28 UTC
    phimtau123:

    It appears that $array[0] is the field name and that you're using regular expressions to get case insensitivity. If that's the case, you could let too much junk through since your switch statement is looking for words with any matching substring. You might instead just change your code to map things to upper or lower case for the comparison, like:

    switch (uc($array[0])) { case (uc("Time_Stamp")) { @time = split (/-/, $array[1]); $date_submited = $time[0];
    Also, you should quit putting quotes around your variable references. It slows things down, and can cause you some problems. For example, check the result of the following line when @lotid contains 0, 2, 4.
    $wafer_flow = "$lotid[1]-$lotid[2]";
    ...roboticus
Re^3: Case Statement
by ikegami (Patriarch) on May 28, 2007 at 18:00 UTC

    Assuming /Time_Stamp/i really means /^Time_Stamp\z/i,

    my @array = ...; my %dispatch = ( time_stamp => sub { @time = split (/-/, $array[1]); $date_submited = $time[0]; $time_submited = $time[1]; }, ... ); my $func = $dispatch{lc($array[0])}; if ($func) { $func->(); } else { ... }