简体   繁体   English

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

[英]Is it possible to somehow shorten switch statements? Maybe with a loop?

Independent of which programming language, what options are there to shorten long switch statements with many similar cases? 与哪种编程语言无关,在许多类似情况下,有哪些选择可以缩短长switch语句?

From what I've searched, I found answers like this one but my cases are all different with only one integer changing like in a loop. 从我所搜索的内容,我发现喜欢回答这一个 ,但我的情况是只有一个整数,在一个循环中改变像所有不同。

Regularly I use following switch statement construct in bash/PHP/Python/JavaScript and am looking for a shorter version: 我经常在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;
}

In Python you can use something as simple as this, if your return value is that simple: 在Python中,如果您的返回值就是这么简单,则可以使用像这样简单的东西:

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

Or in case you expect something more complicated in your return value: 或者,如果您期望返回值更复杂一些:

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

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

In JavaScript: 在JavaScript中:

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

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

Try this with PHP 使用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;

}

I suggest to use an indexed array with bash: 我建议使用带有bash的索引数组:

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

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

Use an array to store 'cases' as keys and return values as value. 使用数组将“ case”存储为键,并返回值作为value。 Then it will be possible to check if a given key exists in the array. 然后可以检查数组中是否存在给定键。 If YES then return its value, if not then return default value. 如果是,则返回其值,如果不是,则返回默认值。

I don't know php so this is in python: 我不知道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

If the number is ordered. 如果订购该号码。 There is no better way than to use array indexing. 没有比使用数组索引更好的方法了。

On the other hand, if the values differ, the solution can be done with a simple implementation of a dictionary / HashMap / associative array. 另一方面,如果值不同,则可以通过简单实现字典/ HashMap /关联数组来完成该解决方案。

In Python, you could use a dictionary as done below 在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]

If you need a solution independent of any specific programming language, you might try JSON ( Javascript Object Notation ). 如果需要独立于任何特定编程语言的解决方案,则可以尝试JSONJavascript Object Notation )。

Despite its name, JSON is a language-agnostic, text-based, data-interchange format, much like XML, CSV (or YAML). 尽管有其名称, JSON是一种与语言无关的,基于文本的数据交换格式,与XML,CSV(或YAML)非常相似。

Being language-independent, JSON may be interrogated and processed not only by Javascript, but also by Java, Python, PHP and many other languages. 与语言无关, JSON不仅可以通过Javascript进行查询,而且可以通过Java,Python,PHP和许多其他语言进行查询和处理。

Multiple languages can refer to and process the same JSON data. 多种语言可以引用和处理相同的JSON数据。

A JSON example of the data in the question: 问题中数据的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
  }
}

Thank you very much for your contributions, I could come up with following examples. 非常感谢您的贡献,我可以举几个例子。 I hope they are the shortest possible variants for their language, if not, feel free to edit/correct. 我希望它们是其语言的最短变体,如果不是,请随时进行编辑/更正。

Python dev2host.py: 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 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: 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: 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")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM