我有一个包含此文件的文件:
PQRParrot, Quagga, Raccoon
DEFDo statements, Else statements, For statements
GHIGeese, Hippos, If statements
YZ Yak, Zebra
JKLJelly Fish, Kudu, Lynx
MNOManatee, Nautilus, Octopus
ABCApples, Boas, Cats
VWXVulture, While statements, Xmen
STUSea Horse, Tapir, Unicorn
我需要在这样的表格中显示它:
Key Data
ABC Apples, Boas, Cats
DEF Do statements, Else statements, For statements
GHI Geese, Hippos, If statements
JKL Jelly Fish, Kudu, Lynx
MNO Manatee, Nautilus, Octopus
PQR Parrot, Quagga, Raccoon
STU Sea Horse, Tapir, Unicorn
VWX Vulture, While statements, Xmen
YZ Yak, Zebra
这是我到目前为止的代码:
lines = File.open("file.txt").read.split
fHash = {}
lines.each do |line|
next if line == ""
fHash[line[0..2]] = line[3..-1]
end
f = File.open("file.txt")
fHash = {}
loop do
x = f.gets
break unless x
fHash[x[0..2]] = x[3..-1]
end
fHash = fHash.to_a.sort.to_h
puts fHash
f.close
这就是代码输出的内容:
{ "ABC" => "Apples, Boas, Cats\n",
"DEF" => "Do statements, Else statements, For statements\n",
"GHI" => "Geese, Hippos, If statements\n",
"JKL" => "Jelly Fish, Kudu, Lynx\n",
"MNO" => "Manatee, Nautilus, Octopus\n",
"PQR" => "Parrot, Quagga, Raccoon\n",
"STU" => "Sea Horse, Tapir, Unicorn\n",
"VWX" => "Vulture, While statements, Xmen\n",
"YZ " => "Yak, Zebra\n"
}
所以我要做的是读取文件的内容,取前三个字符并将其设置为键,其余为数据,按键值对哈希进行排序,然后将数据显示为表
我环顾四周,找到了一些与我的问题类似的东西,但对我来说没什么用。
答案 0 :(得分:2)
我认为你是在思考这个问题。如果你有一个包含这些内容的文件,要打印表,你需要做的就是在每行的第三个字符后面插入一个空格,然后对它们进行排序(或者相反)。这很简单:
lines = File.foreach("homework02.txt")
.map {|line| line.insert(3, " ") }
puts "Key Data"
puts lines.sort
如果你想从文件的行中构建一个Hash,你所要做的就是:
hsh = File.foreach("homework02.txt")
.map {|line| [ line.slice!(0,3), line ] }
.sort.to_h
这构建了一个两元素数组的数组,其第一个元素是每行的前三个字符,第二个元素是该行的其余部分,然后对其进行排序并将其转换为哈希值。
然后,打印你的桌子:
puts "Key Data"
puts hsh.map {|key, val| "#{key} #{val}" }
答案 1 :(得分:0)
我通过改变它的排序方式来实现它。更新了以下代码。
lines = File.open("homework02.txt").read.split
fHash = {}
lines.each do |line|
next if line == ""
fHash[line[0..2]] = line[3..-1]
end
f = File.open("homework02.txt")
fHash = {}
loop do
x = f.gets
break unless x
fHash[x[0..2]] = x[3..-1]
end
fHash = Hash[fHash.sort_by { |k, v| k }]
print "Key ", " Data \n"
fHash.each do |key, val|
print key, " ", val
end
f.close
答案 2 :(得分:0)
我假设每一行都以一个或多个大写字母开头,后跟一个可选空格,后跟一个大写字母,后跟一个小写字母。
<强>代码强>
R = /
\A[A-Z]+ # Match start of string followed by one or more capital letters
\K # Forget everything matched so far
(?=[A-Z][a-z]) # Match a capital letter followed by a lowercase letter
# in a postive lookahead
/x # Extended/free-spacing regex definition mode
逐行读取文件,格式化每一行,对第一个空格中的每一行进行分区并排序:
def make_string(fname)
File.foreach(fname).map { |s| s.gsub(R, ' ').chomp.partition(' ') }.
sort.
map(&:join)
end
如果您希望创建指定的哈希,可以写:
def make_hash(fname)
File.foreach(fname).map { |s| s.gsub(R, ' ').chomp.partition(' ') }.
sort.
map { |f,_,l| [f,l] }.
to_h
end
在正则表达式中,字符串的第一部分无法在正向后观中匹配,因为匹配是可变长度的。这就是我使用\K
的原因,它没有这个限制。
<强>实施例强>
首先,让我们创建文件:
str = <<_
PQRParrot, Quagga, Raccoon
DEFDo statements, Else statements, For statements
GHIGeese, Hippos, If statements
YZ Yak, Zebra
JKLJelly Fish, Kudu, Lynx
MNOManatee, Nautilus, Octopus
ABCApples, Boas, Cats
VWXVulture, While statements, Xmen
STUSea Horse, Tapir, Unicorn
_
FName = 'temp'
File.write(FName, str)
#=> 265
然后
puts make_string(FName)
ABC Apples, Boas, Cats
DEF Do statements, Else statements, For statements
GHI Geese, Hippos, If statements
JKL Jelly Fish, Kudu, Lynx
MNO Manatee, Nautilus, Octopus
PQR Parrot, Quagga, Raccoon
STU Sea Horse, Tapir, Unicorn
VWX Vulture, While statements, Xmen
YZ Yak, Zebra
make_hash(FName)
#=> {"ABC"=>"Apples, Boas, Cats",
# "DEF"=>"Do statements, Else statements, For statements",
# "GHI"=>"Geese, Hippos, If statements",
# "JKL"=>"Jelly Fish, Kudu, Lynx",
# "MNO"=>"Manatee, Nautilus, Octopus",
# "PQR"=>"Parrot, Quagga, Raccoon",
# "STU"=>"Sea Horse, Tapir, Unicorn",
# "VWX"=>"Vulture, While statements, Xmen",
# "YZ"=>"Yak, Zebra"}
作为第二个例子,假设:
str = <<_
PQRSTUVParrot, Quagga, Raccoon
DEFDo statements, Else statements, For statements
Y Yak, Zebra
_
FName = 'temp'
File.write(FName, str)
#=> 94
然后
puts make_string(FName)
PQRSTUV Parrot, Quagga, Raccoon
Y Yak, Zebra
make_hash(FName)
# => {"DEF"=>"Do statements, Else statements, For statements",
# "PQRSTUV"=>"Parrot, Quagga, Raccoon", "Y"=>"Yak, Zebra"}