I was actually trying to print multiple values of the same attribute Title with comma after each but couldn't remove comma after the last value which resulted in me writing complex loops.
Here I have initiated a count variable storeCount which increments and until it reaches the number of rows returned, I print Title and a comma that follows.
Here's my code. I am getting syntax error. I would appreciate help fixing the code.
<?for-each#inlines:G_1?>
<?xdoxslt:set_variable($_XDOCTX, 'storeCounter', xdoxslt:get_variable($_XDOCTX, 'storeCounter') + 1)?>
Title
<?if#inlines:<?count(G_1)?> != <?xdoxslt:get_variable($_XDOCTX, 'storeCounter')?>?>
,
<?end-if?>
<?end for-each?>
This should work:
<?for-each#inlines:G_1?>
<?xdoxslt:set_variable($_XDOCTX, 'storeCounter', xdoxslt:get_variable($_XDOCTX, 'storeCounter') + 1)?>
Title
<?if#inlines:count(G_1) != xdoxslt:get_variable($_XDOCTX, 'storeCounter')?>
,
<?end-if?>
<?end for-each?>
You should set the variable to 1 or something before the loop;
Right now, you're essentially getting a non-existent variable and adding 1 to
it on the first record.
You cant have within an existing
Related
I have a dataset with a number of columns. Two of them are practically the same however in variable column 1 there are string data that I would like to extract and replace in empty cells of variable column 2.
I tried using the syntax
If
variable_2 = "".
Compute variable_1 = variable_2.
End If
But do not get anything. Please, could someone help with this?
Much appreciated.
This should be either
if var2="" var2=var1.
(no period after the condition, no "end if")
OR
do if var2="".
compute var2=var1.
end if.
(this is a "do if" and not just an "if" - enables you to add commands after the condition, and not needed here).
In any case, if variable_2 is empty you want to run variable_2=variable_1 and not the reverse.
I'd like to know how to turn different variables with specific values into a new category variable to extend the specific values? I tried it with compute a variable to get a new one but it didn't work:
this is what I tried but it didn't work:
DO IF ((Q103_01=>3) AND (Q103_05=>3) AND (Q201_06=<2) AND (Q201_07=<2) AND (Q201_08=<2) AND (Q301_06=<2) AND (Q301_07=<2) AND (Q301_08=<2) AND (Q305_04=2) AND (Q305_06=2) AND (Q409_05=1) AND (Q407_07=<2) AND (Q408_04=1) AND (Q408_05=1) AND (Q411_02=<2) AND (Q203_04=<2) AND (Q203_06=<2) AND (Q203_07=<2) AND (Q203_09=<2) AND (Q203_10=<2)).
COMPUTE NewVariable = 1.
END IF.
EXECUTE.
Thanks for help in advance!
When using "Less than or equal to" or "Greater than or equal to" the sequence should be <= (or LE), >= (or GE).
The error message came from using => instead of >=.
Otherwise your syntax should work fine.
But it can be written more efficiently this way:
if cond1 and cond2 and cond3 newvar=1.
without using do if.
I am trying to calculate the average of parameters by passing them to an array but I am getting the following error. So I have doubt am I doing it right?
Here is my code
arr= #r1+#r2+#r3+#r4+#r5
#current_user_satisfaction= arr.inject{ |sum, el| sum + el }.to_i / arr.size
Where I am getting r1, r2,r3,r4 from the DB.
I am getting the following error
: syntax error, unexpected tIDENTIFIER, expecting keyword_end #current_user satisfaction= arr.inject{ |sum, el| sum +... ^
What was my problem where is my error. I actually started learning ROR very recently.
This what I am writing to do
I have as set of variables which I am taking from db based up user_id and company_id
the variables are
#r1=company_rating.collect(&:r1)
#r2=company_rating.collect(&:r2)
#r3=company_rating.collect(&:r3)
#r4=company_rating.collect(&:r4)
And I wanted to find the avg of all those variables.
So I am doing it like arr = [#r1,#r2,#r3,®r4]
#current-user_satisfaction= arr.sum.compact /arr.size
But I am getting an error / is undefined and Why I am doing compact is because I have sum of the nil values in that
So please help how do this.
This is not an array!
arr= #r1+#r2+#r3+#r4+#r5
This is a sum. Should be arr= [#r1,#r2,#r3,#r4,#r5]
Additionally you missed dot between #current_user and satisfaction.
Also it is not as it should be done in rails. Could you show us how those variables are set?
First, You need to create the array like this
arr= [#r1,#r2,#r3,#r4,#r5]
The error you are getting is about the . dot you missed between #current_user and satisfaction. Change it accordingly and you will get it working.
As far as I think, you don't need an array there. if you are doing, arr= #r1+#r2+#r3+#r4+#r5, it is already calculating the sum of values, why you need to put them to array and go for a loop.
However, If you still want to use array, instead of arr.inject{ |sum, el| sum + el }, you can use arr.reduce('+'), which does the same thing of calculating sum of all array values. This has nothing to do with your problem, but makes your code more understandable.
Hi I am find the average
This what I am trying to do
I have as set of variables which I am taking from db based upon user_id and company_id the variables are And I cannot added then and there because I need to display individual parameter in my show page and I also wanted to display their average
So I am trying to do as below
#r1=company_rating.collect(&:r1)
#r2=company_rating.collect(&:r2)
#r3=company_rating.collect(&:r3)
#r4=company_rating.collect(&:r4)
So I am doing it like
arr = [#r1,#r2,#r3,®r4]
#totalaverage= arr.sum.compact /arr.size
My array sample looks like [10,20,30,nil],[nil,nil,nil,nil],[30,40,50,60]
And If I have array all Nil then it should show be Nil
But I am getting an error undefined method `/' for # and Why I am doing compact is because I have sum of the nil values in that
So please help how do this.
First of all, you define arr as an array of arrays. #r1, #r2 etc. are all arrays and what [#r1, #r2, ...] does is just mixing them up in another array. You probably want to merge them, not include them in another array:
arr = #r1 + #r2 + #r3 + #r4
Second, you should call arr.compact first, then sum the contents up. Also, I'm not really sure about the sum method. I'd use reduce(&:+) instead. So, to answer your question, '/' fails because compact returns an Array, and you're trying to divide an Array to a number. This looks better:
arr = #r1 + #r2 + #r3 + #r4
#totalaverage = arr.compact.reduce(&:+) / arr.size
What Array#reduce(&:+) does is to apply the + operator between array members and return the value (not an array).
EDIT: arr.sum does work if you're using Ruby on Rails. Otherwise use arr.reduce(&:+) instead.
Do as below,which should work :
#totalaverage= arr.flat_map(&:compact).inject(:+) /arr.size.to_f
Actually #totalaverage is an array of array. Where each internal element(array) of #totalaverage can have nil values also(as you shown). So you need to remove those nil entries if any from the internal array of #totalaverage. And arr.map(&:compact) will do the same job.
Simplest way to do this if you're using Rails:
#totalaverage = a.flatten.compact.sum.to_f / a.flatten.compact.size if a.flatten.compact.present?
This will assign to #totalaverage the result or nil in case all the values are nil.
I have a custom SQL query that I run with this line:
#avg_score = "#{ActiveRecord::Base.connection.execute(#avg_score_SQL)}"
when that is run, #avg_score has a value of:
[{"score"=>8, 0=>8}]
the results are correct, but it prints it like that :-/ I just want it to print out the "8"
How do I get it to just print the value of score?
[{"score"=>8, 0=>8}] is just an array with one element, and that element is a hash. So you can access the score like this:
#avg_score[0]['score']
if you use execute method, you have to free the Result object after you're done using it.
Also your statement can be used without quotes.
if you always have 1 result row, you should better use selecy_one
#avg_score = ActiveRecord::Base.connection.select_one(#avg_score_SQL)['score']