来自输入文件,其中包含多行'name = value'字段(前20行)
Job=C201A005 include=PC201
Job=C201A005 proc=RUNTRACN
Job=C201A005 proc=RUNTRACN utilpgm=IEFBR14
Job=C201A005 proc=DD0005
Job=C201A005 proc=DD0005 utilpgm=SORT
Job=C201A005 proc=DD0005 include=SORT
Job=C201A005 proc=DD0005 file=PC201.AD.SRVCPT.INPUT
Job=C201A005 proc=DD0005 pdsmem=ULDH.REHOST3.PARM/SA005
Job=C201A005 proc=DD0005 include=BATC2P
Job=C201A005 proc=DD0005 include=BATC2P file=ULDH.REHOST3.LOADBTCH
Job=C201A005 proc=DD0005 include=BATC2P file=SYS2.DB2DBP1.SDSNLOAD
Job=C201A005 proc=DD0005 include=BATC2P file=SYS2.DB2DBP1.RUNLIB.LOAD
Job=C201A005 proc=DD0005 include=BATC2P file=SYS2.S99I0062.PROD.SCSQLOAD
Job=C201A005 proc=DD0005 include=BATC2P file=ULDH.REHOST3.REXX
Job=C201A005 proc=DD0005 include=BATC2P file=ULDH.REHOST3.REXX
Job=C201A005 proc=ABENDJOB
Job=C201A005 proc=ABENDJOB utilpgm=IEFBR14
Job=C201A005 proc=ABENDJOB file=PROD.NET.MSGS
Job=C201A018 include=PC201
Job=C201A018 proc=RUNTRACN
Job=C201A005 include=PC201
Job=C201A005 proc=RUNTRACN
Job=C201A005 proc=RUNTRACN utilpgm=IEFBR14
Job=C201A005 proc=DD0005
Job=C201A005 proc=DD0005 utilpgm=SORT
Job=C201A005 proc=DD0005 include=SORT
Job=C201A005 proc=DD0005 file=PC201.AD.SRVCPT.INPUT
Job=C201A005 proc=DD0005 pdsmem=ULDH.REHOST3.PARM/SA005
Job=C201A005 proc=DD0005 include=BATC2P
Job=C201A005 proc=DD0005 include=BATC2P file=ULDH.REHOST3.LOADBTCH
Job=C201A005 proc=DD0005 include=BATC2P file=SYS2.DB2DBP1.SDSNLOAD
Job=C201A005 proc=DD0005 include=BATC2P file=SYS2.DB2DBP1.RUNLIB.LOAD
Job=C201A005 proc=DD0005 include=BATC2P file=SYS2.S99I0062.PROD.SCSQLOAD
Job=C201A005 proc=DD0005 include=BATC2P file=ULDH.REHOST3.REXX
Job=C201A005 proc=DD0005 include=BATC2P file=ULDH.REHOST3.REXX
Job=C201A005 proc=ABENDJOB
Job=C201A005 proc=ABENDJOB utilpgm=IEFBR14
Job=C201A005 proc=ABENDJOB file=PROD.NET.MSGS
Job=C201A018 include=PC201
Job=C201A018 proc=RUNTRACN
我正在尝试构建一个数据结构来为每个“Job”保存一个每种类型的数组结构(proc,include,file,program,utilpgm ..)'
这是我写的代码。我无法在适当的数组上推送'值'。
#!/usr/bin/ruby
jobs={} #holds everything
currentjob=""
files=[], pdsmems=[], includs=[], procs=[], programs=[], utilpgms=[]
ARGF.each do |line|
# chop the line into fields,
# where every field is a 'name=value' pair.
line.chomp!
fields = line.split(' ')
print "Currentjob = >#{currentjob}<\n"
for field in fields do
fieldname, value = field.split('=') # split into name, value
#print ">> #{fieldname}=#{value}\n"
if fieldname =~ /[jJ]ob/
if currentjob != value
print "NEW JOB #{fields[0].split('=')[1]}\n"
# this is a new job, so build the hash for old one
files=[], pdsmems=[], includs=[], procs=[], programs=[], utilpgms=[]
jobelements = {'file' => files, 'pdsmem' => pdsmems,
'include' => includs, 'proc' => procs,
'program' => programs, 'utilpgm' => utilpgms}
jobs[value] = jobelements
#print "Hash assigned. job = #{jobs} \n"
currentjob = value
end
next
end
print "fieldname = #{fieldname}\n"
# push 'value' on the appro. array in the 'jobelements' hash
# which is value of the 'currentjob' key.
#jobs[currentjob][fieldname].push(value) # <== HELP!
#jobs[currentjob][fieldname] << value # <== HELP!
end
end
#print the data structure
print jobs
我似乎无法正确地引用正确的数组并将elemnt推入其中。 帮助赞赏。 干杯! P.S。
预期输出如下:
#!/usr/bin/ruby
jobs={} #holds everything
currentjob=""
files=[], pdsmems=[], includs=[], procs=[], programs=[], utilpgms=[]
ARGF.each do |line|
# chop the line into fields,
# where every field is a 'name=value' pair.
line.chomp!
fields = line.split(' ')
print "Currentjob = >#{currentjob}<\n"
for field in fields do
fieldname, value = field.split('=') # split into name, value
#print ">> #{fieldname}=#{value}\n"
if fieldname =~ /[jJ]ob/
if currentjob != value
print "NEW JOB #{fields[0].split('=')[1]}\n"
# this is a new job, so build the hash for old one
files=[], pdsmems=[], includs=[], procs=[], programs=[], utilpgms=[]
jobelements = {'file' => files, 'pdsmem' => pdsmems,
'include' => includs, 'proc' => procs,
'program' => programs, 'utilpgm' => utilpgms}
jobs[value] = jobelements
#print "Hash assigned. job = #{jobs} \n"
currentjob = value
end
next
end
print "fieldname = #{fieldname}\n"
# push 'value' on the appro. array in the 'jobelements' hash
# which is value of the 'currentjob' key.
#jobs[currentjob][fieldname].push(value) # <== HELP!
#jobs[currentjob][fieldname] << value # <== HELP!
end
end
#print the data structure
print jobs
答案 0 :(得分:0)
以下是一些如何创建数组和哈希的基础知识:
#create empty array called jobs
jobs = []
#or use jobs = Array.new
#create a hash from one of your lines:
line = {"Job" => "C201A018", "proc" => "RUNTRACN" }
#add the line to your jobs
jobs.push(line)
#or do it this way:
#jobs << line
答案 1 :(得分:0)
初始化数组变量的方式可能无法按照您希望的方式工作。
files=[], pdsmems=[], includs=[], procs=[], programs=[], utilpgms=[]
不会创建一个名为files的新空数组,但文件将如下所示:
[[], [], [], [], [], []]
这是因为,
不是语句的结尾,而是会在其后面和之前的所有其他值上创建一个数组。
示例:
a = [1], b = [2], c = [3]
puts a.to_s # returns '[[1], [2], [3]]' and not '[1]'
您可以通过将,
替换为;
或新行来解决此问题。 ;
终止一个语句,就像换行符一样;
要想方便地测试短红宝石片段,请查看pry或irb(可能已经安装了红宝石)。