Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby: pushing element on array inside a Hash
    text
    copied!<p>From an input file with many lines of 'name=value' fields looking like (top 20 lines) <code></p> <pre><code>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 </code></pre> <p></code></p> <p>I'm trying to build a data structure to hold 'for each "Job" a structure of arrays of each type (proc, include,file, program, utilpgm..)' Here is the code I wrote. I'm unable to push a 'value' on the appropriate array. <code></p> <pre><code> #!/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 = &gt;#{currentjob}&lt;\n" for field in fields do fieldname, value = field.split('=') # split into name, value #print "&gt;&gt; #{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' =&gt; files, 'pdsmem' =&gt; pdsmems, 'include' =&gt; includs, 'proc' =&gt; procs, 'program' =&gt; programs, 'utilpgm' =&gt; 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) # &lt;== HELP! #jobs[currentjob][fieldname] &lt;&lt; value # &lt;== HELP! end end #print the data structure print jobs </code></pre> <p></code></p> <p>I can't seem to get the syntax right to reference the correct array and push an elemnt onto it. Help appreciated. Cheers! P.S.</p> <p>The expected output is like below:</p> <pre><code>["C201A005", {"file"=&gt;["PC201.AD.SRVCPT.INPUT", "ULDH.REHOST3.LOADBTCH", "SYS2.DB2DBP1.SDSNLOAD", "SYS2.DB2DBP1.RUNLIB.LOAD", "SYS2.S99I0062.PROD.SCSQLOAD", "ULDH.REHOST3.REXX", "PROD.NET.MSGS"], "pdsmem"=&gt;["ULDH.REHOST3.PARM/SA005"], "include"=&gt;["PC201", "SORT", "BATC2P"], "proc"=&gt;["RUNTRACN", "DD0005", "ABENDJOB"], "program"=&gt;[], "utilpgm"=&gt;["IEFBR14", "SORT"]}] ["C201A018", {"file"=&gt;[], "pdsmem"=&gt;[], "include"=&gt;["PC201"], "proc"=&gt;["RUNTRACN"], "program"=&gt;[], "utilpgm"=&gt;[]}] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload