SID-02074: Construction of hash with list elements
| Status: |
Answered |
TWiki version: |
6.0.0 |
Perl version: |
|
| Category: |
SpreadSheetPlugin |
Server OS: |
Ubuntu Linux 14.01 |
Last update: |
10 years ago |
This question is extends my
previous question
where I wanted to display the table containing
format="|$formfield(paper_title) | $formfield(conference) | $percntFORMFIELD{ \"deadline_abstract\" topic=\"$formfield(conference)\" }$percnt |"
became the hint to use hashes to make the retrieval more efficient. This works perfect!
Now I want to provide the inverse view to the "conference - paper" relation.
In particular, I want to generate a table of upcoming conferences, and provide a list of papers submitted to each of them.
Using hashes, I thought it might be efficient to scan the list of conference papers and incrementally construct a hash called
map_conf2paper
in the mimics of $SETMHASH function.
Assuming that there is a list papers and conferences and a hash
mapping
map_papers2conf
associating each paper with the corresponding conference I want to construct a mapping one-to-many mapping between conferences and papers.
%CALCULATE{
$SETLIST(swallow_comma,
$LISTEACH(
$SETMHASH(map_conf2paper, $GETHASH(map_papers2conf, $item), +$item),
$GETLIST(papers)
)
)
}%
I am using $SETMHASH as a placeholder to indicated the required cumulation of values associated with a single hash key. I tried to use $INSERTSTRING and access the hash map_conf2paper within the $LISTEACH to incrementally append to a string values, but apparently the hash variable constructed using $SETHASH is available only when the variable finished the job. Many thanks for a hint in advance.
--
Rostislav Chudoba - 2015-06-18
Discussion and Answer
You can't use
$SETMHASH in your case; as you noticed it only works with numbers. Here is a working solution with example:
%CALCULATE{
$LIST2HASH(p2c, p10, c1, p11, c1, p12, c1, p12, c2, p12, c3, p20, c2, p21, c2, p30, c3, p31, c3)
$SETLIST(tmp,
$LISTEACH(
$SET(c, $GETHASH(p2c, $item))
$SETHASH(
c2p,
$GET(c),
$item $GETHASH(c2p, $GET(c))
),
$GETHASH(p2c)
)
)
$GETHASH(c2p, c3)
This example returns papers
p31 p30 p12 for conference
c3. The papers are concatenated by space:
$item $GETHASH(c2p, $GET(c)); you could concatenate with comma-space, but then you get a trailing empty list item, which you can strip using
$LISTNONEMPTY() when you retrieve a hash element containing a list of papers.
--
Peter Thoeny - 2015-06-23
If you answer a question - or someone answered one of your questions - please remember to edit the page and set the status to answered. The status selector is below the edit box.