我的函数内部有一个 DataFrame:
using DataFrames
myservs = DataFrame(serverName = ["elmo", "bigBird", "Oscar", "gRover", "BERT"],
ipAddress = ["12.345.6.7", "12.345.6.8", "12.345.6.9", "12.345.6.10", "12.345.6.11"])
myservs
5x2 DataFrame
| Row | serverName | ipAddress |
|-----|------------|---------------|
| 1 | "elmo" | "12.345.6.7" |
| 2 | "bigBird" | "12.345.6.8" |
| 3 | "Oscar" | "12.345.6.9" |
| 4 | "gRover" | "12.345.6.10" |
| 5 | "BERT" | "12.345.6.11" |
如何编写函数来获取名为 server
的单个参数,不区分大小写,与 myservs[:serverName] 中的
code> DataArray,并返回匹配对应的server
参数匹配ipAddress
?
在 R 中,这可以通过使用来完成
myservs$ipAddress[grep("server", myservs$serverName, ignore.case = T)]
我不希望有人使用 ElMo
或 Elmo
作为 服务器
,或者 serverName
保存为 elmo
或 ELMO
。
最佳答案
我引用了如何在 R 中完成任务并尝试使用 DataFrames
pkg 来完成此任务,但我这样做只是因为我来自 R
并且我刚刚学习Julia
。我向同事询问了很多问题,以下是我们得出的结论:
This task is much cleaner if I was to stop thinking in terms of
vectors
inR
.Julia
runs plenty fast iterating through a loop.Even still, looping wouldn't be the best solution here. I was told to look into Dicts (check here for an example).
Dict()
,zip()
,haskey()
, andget()
blew my mind. These have many applications.My solution doesn't even need to use the
DataFrames
pkg, but instead uses Julia'sMatrix
andArray
data representations. By usinglet
we keep the global environment clutter free and the server name/ip list stays hidden from view to those who are only running the function.In the sample code, I'm recreating the server matrix every time, but in reality/practice I'll have a permission restricted delimited file that gets read every time. This is OK for now since the delimited files are small, but this may not be efficient or the best way to do it.
# ONLY ALLOW THE FUNCTION TO BE SEEN IN THE GLOBAL ENVIRONMENT
let global myIP
# SERVER MATRIX
myservers = ["elmo" "12.345.6.7"; "bigBird" "12.345.6.8";
"Oscar" "12.345.6.9"; "gRover" "12.345.6.10";
"BERT" "12.345.6.11"]
# SERVER DICT
servDict = Dict(zip(pmap(lowercase, myservers[:, 1]), myservers[:, 2]))
# GET SERVER IP FUNCTION: INPUT = SERVER NAME; OUTPUT = IP ADDRESS
function myIP(servername)
sn = lowercase(servername)
get(servDict, sn, "That name isn't in the server list.")
end
end
# Test it out
myIP("SLIMEY")
#>"That name isn't in the server list."
myIP("elMo")
#>"12.345.6.7"
关于regex - DataArray 不区分大小写的匹配,返回匹配的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426542/