有可能以某种方式缩短switch语句吗?也许有一个循环?

时间:2016-11-24 12:13:24

标签: javascript php python bash switch-statement

独立于哪种编程语言,有哪些选项可以缩短具有许多类似情况的长切换语句?

从我搜索过的内容中,我找到了this one之类的答案,但我的情况都不同,只有一个整数在循环中发生变化。

我经常在bash / PHP / Python / JavaScript中使用以下switch语句构造,并且正在寻找更短的版本:

switch ($device) {
    // 2ghz
    case "n2":
        return 1;
        break;
    case "nne2":
        return 2;
        break;
    case "ne2":
        return 3;
        break;
    case "ene2":
        return 4;
        break;
    case "e2":
        return 5;
        break;
    case "ese2":
        return 6;
        break;
    case "se2":
        return 7;
        break;
    case "sse2":
        return 8;
        break;
    case "s2":
        return 9;
        break;
    case "ssw2":
        return 10;
        break;
    case "sw2":
        return 11;
        break;
    case "wsw2":
        return 12;
        break;
    case "w2":
        return 13;
        break;
    case "wnw2":
        return 14;
        break;
    case "nw2":
        return 15;
        break;
    case "nnw2":
        return 16;
        break;

    // 5ghz
    case "n5":
        return 17;
        break;
    case "nne5":
        return 18;
        break;
    case "ne5":
        return 19;
        break;
    case "ene5":
        return 20;
        break;
    case "e5":
        return 21;
        break;
    case "ese5":
        return 22;
        break;
    case "se5":
        return 23;
        break;
    case "sse5":
        return 24;
        break;
    case "s5":
        return 25;
        break;
    case "ssw5":
        return 26;
        break;
    case "sw5":
        return 27;
        break;
    case "wsw5":
        return 28;
        break;
    case "w5":
        return 29;
        break;
    case "wnw5":
        return 30;
        break;
    case "nw5":
        return 31;
        break;
    case "nnw5":
        return 32;
        break;

    // 24ghz
    case "n24":
        return 33;
        break;
    case "nne24":
        return 34;
        break;
    case "ne24":
        return 35;
        break;
    case "ene24":
        return 36;
        break;
    case "e24":
        return 37;
        break;
    case "ese24":
        return 38;
        break;
    case "se24":
        return 39;
        break;
    case "sse24":
        return 40;
        break;
    case "s24":
        return 41;
        break;
    case "ssw24":
        return 42;
        break;
    case "sw24":
        return 43;
        break;
    case "wsw24":
        return 44;
        break;
    case "w24":
        return 45;
        break;
    case "wnw24":
        return 46;
        break;
    case "nw24":
        return 47;
        break;
    case "nnw24":
        return 48;
        break;

    default:
        return 0;
        break;
}

9 个答案:

答案 0 :(得分:3)

在Python中,如果你的返回值很简单,你就可以使用这么简单的东西:

items = ["n2","nne2","ne2","ene2", ...]
for idx, key in enumerate(items, start=1):
    if device == key:
        result = idx
    result = 0

或者,如果您希望返回值更复杂:

my_dict = {
    "n2": 1,
    "nne2": 2,
    "ne2": 3,
    "ene2": 4,
    ...}

for key, value in my_dict.items():
    if device == key:
        result = value
    result = 0

答案 1 :(得分:2)

在JavaScript中:

var cases = {
   "n2"   : 1,
   "nne2" : 2,
   "ne2"  : 3,
   "ene2" : 4,
   "e2"   : 5,
   "ese2" : 6,
   // and so on
};

var myCase = cases[$device] || 0;

答案 2 :(得分:1)

尝试使用PHP

function yourFunction($devices) {
    $array = ["n2","nne2","ne2","ene2","e2","ese2","se2","sse2","s2","ssw2","sw2","wsw2"];

    if($key = array_search($devices, $array))
        return $key+1;

}

答案 3 :(得分:1)

我建议使用带有bash的索引数组:

declare -A a=([n2]='1' [nne2]='2' [ne2]='3')
device="nne2"

return "${a[$device]}"   # returns with value 2

答案 4 :(得分:0)

使用数组存储'个案'作为键并返回值作为值。 然后,可以检查数组中是否存在给定的键。如果是,则返回其值,否则返回默认值。

答案 5 :(得分:0)

我不知道php所以这是在python:

devices = ["n2", "nne2" ...] # populate it with your devices
...

def foo(device):
    if device in devices:
        return devices.index(device) + 1
    else:
        return 0

答案 6 :(得分:0)

如果订购号码。没有比使用数组索引更好的方法了。

另一方面,如果值不同,可以使用字典/ HashMap /关联数组的简单实现来完成解决方案。

在Python中,您可以使用字典,如下所示

keyList = ['n2','nne2',...] 
valueList = [1, 2, 3, ...]  # Must be same size as keyList
# Best to assign both the above lists 
# with files if there are 50+ entries

deviceDict = {}

for i in range(len(keyList)):
    deviceDict[keyList[i]] = valueList[i]

答案 7 :(得分:0)

如果您需要独立于任何特定编程语言的解决方案,您可以尝试 JSON Javascript Object Notation )。

尽管名称如此, JSON 是一种与语言无关,基于文本的数据交换格式,非常类似于XML,CSV(或YAML)。

与语言无关, JSON 不仅可以通过Javascript进行查询和处理,还可以通过Java,Python,PHP和许多其他语言进行查询和处理。

多种语言可以引用和处理相同的 JSON 数据。

问题中数据的 JSON 示例:

{
  "2ghz": {
    "n2": 1,
    "nne2": 2,
    "ne2": 3,
    "ene2": 4,
    "e2": 5,
    "ese2": 6,
    "se2": 7,
    "sse2": 8,
    "s2": 9,
    "ssw2": 10,
    "sw2": 11,
    "wsw2": 12,
    "w2": 13,
    "wnw2": 14,
    "nw2": 15,
    "nnw2": 16
  },

  "5ghz": {
    "n5": 17,
    "nne5": 18,
    "ne5": 19,
    "ene5": 20,
    "e5": 21,
    "ese5": 22,
    "se5": 23,
    "sse5": 24,
    "s5": 25,
    "ssw5": 26,
    "sw5": 27,
    "wsw5": 28,
    "w5": 29,
    "wnw5": 30,
    "nw5": 31,
    "nnw5": 32,
  },

  "24ghz": {
    "n24": 33,
    "nne24": 34,
    "ne24": 35,
    "ene24": 36,
    "e24": 37,
    "ese24": 38,
    "se24": 39,
    "sse24": 40,
    "s24": 41,
    "ssw24": 42,
    "sw24": 43,
    "wsw24": 44,
    "w24": 45,
    "wnw24": 46,
    "nw24": 47,
    "nnw24": 48
  }
}

答案 8 :(得分:0)

非常感谢您的贡献,我可以提出以下示例。我希望它们是语言的最短变体,如果没有,请随时编辑/更正。

Python dev2host.py:

#!/usr/bin/env python
def dev2host(device):
    devices = [
        "n2", "nne2", "ne2", "ene2", "e2", "ese2", "se2", "sse2", "s2", "ssw2", "sw2", "wsw2", "w2", "wnw2", "nw2", "nnw2", # 2ghz
        "n5", "nne5", "ne5", "ene5", "e5", "ese5", "se5", "sse5", "s5", "ssw5", "sw5", "wsw5", "w5", "wnw5", "nw5", "nnw5", # 5ghz
        "n24", "nne24", "ne24", "ene24", "e24", "ese24", "se24", "sse24", "s24", "ssw24", "sw24", "wsw24", "w24", "wnw24", "nw24", "nnw24", # 24ghz
    ]
    for host, key in enumerate(devices, start=1):
        if device == key:
            print host
        result = 0
    return;
dev2host("ne5")

PHP dev2host.php:

<?php
function dev2host($device) {
    $devices = [
        "n2", "nne2", "ne2", "ene2", "e2", "ese2", "se2", "sse2", "s2", "ssw2", "sw2", "wsw2", "w2", "wnw2", "nw2", "nnw2", // 2ghz
        "n5", "nne5", "ne5", "ene5", "e5", "ese5", "se5", "sse5", "s5", "ssw5", "sw5", "wsw5", "w5", "wnw5", "nw5", "nnw5", // 5ghz
        "n24", "nne24", "ne24", "ene24", "e24", "ese24", "se24", "sse24", "s24", "ssw24", "sw24", "wsw24", "w24", "wnw24", "nw24", "nnw24", // 24ghz
    ];
    if ($host = array_search($device, $devices)) {
        return $host+1;
    }
}
echo dev2host("ne5");
?>

Bash dev2host.sh:

#!/bin/bash
function dev2host() {
    declare -A devices=(
        [n2]='1' [nne2]='2' [ne2]='3' [ene2]='4' [e2]='5' [ese2]='6' [se2]='7' [sse2]='8' # 2ghz
        [s2]='9' [ssw2]='10' [sw2]='11' [wsw2]='12' [w2]='13' [wnw2]='14' [nw2]='15' [nnw2]='16' # 2ghz
        [n5]='17' [nne5]='18' [ne5]='19' [ene5]='20' [e5]='21' [ese5]='22' [se5]='23' [sse5]='24' # 5ghz
        [s5]='25' [ssw5]='26' [sw5]='27' [wsw5]='28' [w5]='29' [wnw5]='30' [nw5]='31' [nnw5]='32' # 5ghz
        [n24]='33' [nne24]='34' [ne24]='35' [ene24]='36' [e24]='37' [ese24]='38' [se24]='39' [sse24]='40' # 24ghz
        [s24]='41' [ssw24]='42' [sw24]='43' [wsw24]='44' [w24]='45' [wnw24]='46' [nw24]='47' [nnw24]='48' # 24ghz
    )
    echo "${devices[$1]}"
}
dev2host ne5

Javascript dev2host.js:

function dev2host($device) {
    var devices = {
        "n2":1, "nne2":2, "ne2":3, "ene2":4, "e2":5, "ese2":6, "se2":7, "sse2":8, // 2ghz
        "s2":9, "ssw2":10, "sw2":11, "wsw2":12, "w2":13, "wnw2":14, "nw2":15, "nnw2":16, // 2ghz
        "n5":17, "nnw5":18, "ne5":19, "ene5":20, "e5":21, "ese5":22, "se5":23, "sse5":24, // 5ghz
        "s5":25, "ssw5":26, "sw5":27, "wsw5":28, "w5":29, "wnw5":30, "nw5":31, "nnw5":32, // 5ghz
        "n24":33, "nnw34":34, "ne24":35, "ene24":36, "e24":37, "ese24":38, "se24":39, "sse24":40, // 24ghz
        "s24":41, "ssw24":42, "sw24":43, "wsw24":44, "w24":45, "wnw24":46, "nw24":47, "nnw24":48, // 24ghz
    };
    var host=devices[$device] || 0;
    return host;
}
document.write(dev2host("ne5"));