gpt4 book ai didi

ruby - 使用正则表达式对散列进行行拆分

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:50 24 4
gpt4 key购买 nike

我正在尝试创建一个散列,例如 ubuntu 中的文件/etc/mime.types,它将每一行拆分为空格和“非单词”正则表达式搜索。下面是我的代码。但我无法让它正确输出零值。它们与其他所有东西一起输出,但如果你用 pp 漂亮地打印它们,一些返回为 nil。有什么建议吗?

#!/usr/bin/ruby
mime = Hash.new()

File.open("/etc/mime.types", "r") do |file|
file.each_line do |line|
next if line[0] == ?#
next if line == "\n"
key, value = line.chomp.split(/\W+\s/)
mime[key] = value
end
end

if (mime.has_value?('nil') == true)
mime.sort.each {|key,value| puts "#{key} has no extensions."}
elsif(mime.has_value?('nil') == false)
mime.sort.each {|key, value| puts "#{key} has extensions #{value}"}
end

这只是输出的尾端,最容易看到,因为执行时它就在前面

我希望

video/quicktime has extensions qt mov
**video/vnd.fvt has no extensions**
**video/vnd.motorola.video has no extensions
video/vnd.motorola.videop has no extensions**
video/vnd.mpegurl has extensions mxu
**video/vnd.mts has no extensions no**
video/vnd.nokia.interleaved-multimedia has extensions
**video/vnd.vivo has no extensions**
video/x-flv has extensions flv
video/x-la-asf has extensions lsf lsx
video/x-matroska has extensions mpv mkv
video/x-mng has extensions mng
video/x-ms-asf has extensions asf asx
video/x-ms-wm has extensions wm
video/x-ms-wmv has extensions wmv
video/x-ms-wmx has extensions wmx
video/x-ms-wvx has extensions wvx
video/x-msvideo has extensions avi
video/x-sgi-movie has extensions movie
x-conference/x-cooltalk has extensions ice
x-epoc/x-sisx-app has extensions sisx
x-world/x-vrml has extensions vrm vrml wrl

但是我明白了

video/quicktime has extensions qt mov
video/vnd.fvt has extensions
video/vnd.motorola.video has extensions
video/vnd.motorola.videop has extensions
video/vnd.mpegurl has extensions mxu
video/vnd.mts has extensions
video/vnd.nokia.interleaved-multimedia has extensions
video/vnd.vivo has extensions
video/x-flv has extensions flv
video/x-la-asf has extensions lsf lsx
video/x-matroska has extensions mpv mkv
video/x-mng has extensions mng
video/x-ms-asf has extensions asf asx
video/x-ms-wm has extensions wm
video/x-ms-wmv has extensions wmv
video/x-ms-wmx has extensions wmx
video/x-ms-wvx has extensions wvx
video/x-msvideo has extensions avi
video/x-sgi-movie has extensions movie
x-conference/x-cooltalk has extensions ice
x-epoc/x-sisx-app has extensions sisx
x-world/x-vrml has extensions vrm vrml wrl

最佳答案

字符串 'nil' 和对象 nil 不是一回事。您的 mime.has_value?('nil') 正在询问 Hash 其值之一是否是字符串 'nil',而不是对象 nil。你(认为你)想要这个:

if(mime.has_value?(nil))
mime.sort.each {|key,value| puts "#{key} has no extensions."}
else
mime.sort.each {|key, value| puts "#{key} has extensions #{value}"}
end

has_value?返回 truefalse 因此您无需将其返回值与 truefalse 进行比较,只需 if(mime.has_value?(nil)) ... else 就足够了。

但这仍然无法满足您的需求。您的 if 将声称 all 键没有扩展名,如果其中至少一个没有扩展名,那没有多大意义。你想用这样的东西遍历 mime:

mime.each do |k, v|
if(v.nil?)
puts "#{k} has no extensions"
else
puts "#{k} has extensions #{v}"
end
end

关于ruby - 使用正则表达式对散列进行行拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9510084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com