SID-01721: Problem with SEARCH in nested FOREACH
| Status: |
Answered |
TWiki version: |
5.0.1 |
Perl version: |
|
| Category: |
ForEachPlugin |
Server OS: |
Sun |
Last update: |
12 years ago |
I've boiled my problem down to the following construct:
%FOREACH{"feature" in="A, B, C"}%
%FOREACH{"kind" in="X, Y, Z"}%
$percntSEARCH{ "parent.name = 'UseCasesUnderReview' AND Feature = '$feature' AND Kind = '$kind'" type="query"}$percnt
%NEXT{"kind"}%
%NEXT{"feature"}%
My search returns no results, although I have topics that match the query (Feature and Kind are form fields). The query string that displays in the results is correct.
Searched: parent.name = 'UseCasesUnderReview' AND Feature = 'A' AND Kind = 'X'
If I change my search's query string to substitute a constant for the $kind variable like so:
$percntSEARCH{ "parent.name = 'UseCasesUnderReview' AND Feature = '$feature' AND Kind = 'X'" type="query"}$percnt
The query gets the results I expect. The query string displayed in the result is identical to the first case which fails. If I swap the nesting around so feature is the inner loop and kind is the outer loop the problem moves from the substitution of $kind to the substitution of $feature.
Hoping someone can point out the error of my ways here.
--
John Weber - 2013-06-05
Discussion and Answer
I've been experimenting with this further and decided I really don't know how escapes and variable substitution work. I've developed this simpler example using FOREACH and CALC
| *Feature* | *Kind* |
%FOREACH{"feature" in="A, B2, C33"}%
$percntFOREACH{"kind" in="X, Y2, Z33"}$percnt
$percntCALCULATE{| $feature ($LENGTH($feature)) | $kind ($LENGTH($kind)) |}$percnt
$percntNEXT{"kind"}$percnt
%NEXT{"feature"}%
The values in the feature column display as I would expect and their reported length matches the length of the value. For the kind column, the values display as I expect but the length is always reported as 5 which is the length of the string $kind. How can I get the values from the FOREACH inner and outer variables to be evaluated consistently inside the CALCULATE?
--
John Weber - 2013-06-11
I don't use the FOREACH so I can't give you advise. One thing strikes me though that you will have a very poor performance because the SEARCH will be done repeatedly. Consider doing just one search. You could do a query search with many AND and OR, or you could use a regular expression search. Untested example:
%SEARCH{
"META:TOPICPARENT.name.UseCasesUnderReview;META:FIELD.name=.Feature.*value=.(A|B2|C33);META:FIELD.name=.Kind.*value=.(X|Y2|Z33)"
type="regex"
}%
Details in
VarSEARCH,
RegularExpression.
--
Peter Thoeny - 2013-06-18
Thanks for the suggestion. The problem is not really about search and more about how escapes are evaluated when you have nested variables like in my example. Experimenting further I came up with an example that works as I would like, but I had to create two topics:
Topic
NestedForNext
| *Feature* | *Kind* |
%FOREACH{"feature" in="A, B2, C33"}%
$percntINCLUDE{"FeatureTable" FEATURE="$feature"}$percnt
%NEXT{"feature"}%
Topic
FeatureTable
%FOREACH{"kind" in="X, Y2, Z33"}%
$percntCALCULATE{| %FEATURE% ($LENGTH(%FEATURE%)) | $kind ($LENGTH($kind)) |}$percnt
%NEXT{"kind"}%
When I tried to combine this into a single topic like so:
| *Feature* | *Kind* |
%FOREACH{"feature" in="A, B2, C33"}%
$percntFOREACH{"kind" in="X, Y2, Z33"}$percnt
$percntCALCULATE{| $feature ($LENGTH($feature)) | $kind ($LENGTH($kind)) |}$percnt
$percntNEXT{"kind"}$percnt
%NEXT{"feature"}%
The length of the value in the
Kind column is always reported as 5 which is the length of the string '$kind' even though I see the correct values for $kind displayed in the column.
If FOREACH is the problem, is there a better way to do what I'm trying to do here which doesn't require FOREACH?
--
John Weber - 2013-06-20
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.