Friday, August 24, 2018

Symfony2: Reference string or an array in config yaml

In symfony2 I used config yaml below:

// config.yml
parameters:
  something: 
    content: 
      price:  2.30
      mainText: 'Some text here.'
      redText:  'This is a text here plus price: ' %price%

The %price% is wrong and gives me an error but system tells its an array, so how to point to something['content'][price]?

Solved

Here's how you can do it

parameters:
    something.content.price: 2.30
    something:
        content:
            mainText: 'Some text here.'
            redText:  'This is a text here plus price: %something.content.price%'

Here we only have 2 parameters, something.content.price that contains a float, and something that contains an array.

This means you will only be able to access directly those 2, inside the DI configuration.


Same answer, different post

A bit late, but here is the solution you were looking for :P

// parameters.yml
parameters:
    something: 
    content: 
        price:  2.30
        mainText: 'Some text here.'
        redText:  'This is a text here plus price: '

The way to use the price defined in your array would be something like:

//config.yml 
twig:
    globals:
        fee: content['price']

Explanation:

When you import your parameters file, into your config.yml file automatically you can access to all variables defined there. To keep in mind, when you use the structure you defined.

You are defining an key value pair array called content that contains many key and value pairs, and the way to refer to them is the one described above.

Hope this be useful for those who may be looking for this! :)


Maybe %something.content.price%


Monday, August 20, 2018

What does ‘::’ (double colon) do in javascript for events?

I saw this code and I'm scratching my head trying to decide how it works.


double colon? This is from using a philips speech mike from a web page.

Any idea what this double colon means? It seems like a syntax error to me but it works! (at least in IE).

Solved

I've been able to find an obscure reference in some scanned manual from Microsoft Office Infopath 2003. It appears to be a JScript syntax:

a double colon is used as separator between the script ID and the event name

My guess is that's not part (or no longer part) of Internet explorer's ECMAScript implementation but it belongs (or used to belong) to Microsoft Office's implementation.


This is an extension to the Javascript language implemented by Microsoft. It's purpose is to specify an event handler for a COM object referenced on the page. SpeechMikeControl is the globally-scoped name of the COM (and/or ActiveX) object:

  • either with an OBJECT or some other element, which has an id property of SpeechMikeControl, or
  • a global variable SpeechMikeControl declared somewhere previously in the Javascript

SPMEventButton is the name of the COM event which will be raised by the SpeechMikeControl object under who-knows-what circumstances.

The double colon is an instruction to connect the function body as a handler to the control's event.


Pretty sure it's a syntax error


I'm pretty certain that's not valid Javascript syntax.

If it works in IE but not other browsers, it could possibly be that IE is treating it as another scripting language (maybe VBScript? although I don't recall that having a double colon operator either? Not sure what other language it could be though.)


The question may not be a duplicate of What does ‘::’ (double colon) do in javascript?, but the answer is: it is a syntax error.

In the following:

function SpeechMikeControl::SPMEventButton(lDeviceID, EventId) {

the keyword function in the global context at the start of an expression indicates a function declaration. Following must be an identifier that is the function name. After the name must be an opening grouping operator '(', formal parameter list and closing grouping operator ')'. So between function and () can only be a single identifier of allowable characters (that isn't a reserved word, or future reserved word, but that isn't an issue here).

The ":" (colon) character is a punctuator and can not appear in an identifier. So it must cause a syntax error if the code is treated as javascript.

Perhaps IE has an extension to the language, I don't know ECMAScript well enough to know if that is permissible, but I'd expect not since it will break other implementations.


As mentioned in this answer of What does ‘::’ (double colon) do in javascript?

:: is a ES2016 operator that is shorthand for bind. This answer intends to assist those that have encountered :: since the ES2016 spec, however, does not apply to the context in which this question was asked.


Sunday, August 19, 2018

Random chose N items from array and update it in BASH SCRIPT

I am trying to random choose a number of items of an array and then update it to do it again till the last set:

#! /bin/bash

A=({1..27})
T=${#A[@]} #number of items in the array
N=3 #number of items to be chosen
V=$(($T/$N)) #number of times to loop
echo ${A[@]} " >> ${#A[@]}"
for ((n=0;n<$V;n++)); do 
    A1=()
    for I in `shuf --input-range=0-$(( ${#A[*]} - 1 )) | head -${N}`; do #random chooses N items random
        S=`echo ${A[$I]}` #the chosen items
        #echo $S
        A1+=("$S|") #creates an array with the chosen items 
        A=("${A[@]/$S}") #deletes the the chosen items from array 
    done
    echo ${A[@]} " >> ${#A[@]}"
    echo ${A1[@]} " >> ${#A1[@]}"
done

The type of output I am getting with this code is:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27  >> 27
1 4 5 6 7 8 9 10 11 1 1 14 15 16 17 18 19 1 2 4 5 6 7  >> 27
20| 2| 3|  >> 3
4 6 7 8 9 0 1 4 6 7 8 9 2 4 6 7  >> 27
1| | 5|  >> 3
6 7 8 9 0 1 6 7 8 9 2 6 7  >> 27
| | 4|  >> 3
7 8 9 0 1 7 8 9 2 7  >> 27
6| | |  >> 3
7 8 9 0 1 7 8 9 7  >> 27
| | 2|  >> 3
7 9 0 1 7 9 7  >> 27
8| | |  >> 3
7 9 1 7 9 7  >> 27
| | 0|  >> 3
7 9 1 7 9 7  >> 27
| | |  >> 3
1  >> 27
9| 7| |  >> 3

Any ideas why it works fine in the start and fails in the end??

Solved

There are some improvements that could be made to your script:

  1. Use lowercase letters
  2. Quote your expansion ("${a[@]}").
  3. Remove items by doing unset a[j].
  4. Re-build array with a=("${a[@]}").
  5. No need for head as shuf could produce a counted result with -n.
  6. Avoid the use of backquotes, use $(…) instead.

That will reduce the script to:

#!/bin/bash

t=27                #number of items in the array
n=3                 #number of items to be chosen
a=( $(seq 1 "$t") )
a1=()

while (( ${#a[@]} >= n )); do 
    for j in $(shuf -n "$n" --input-range=0-$((${#a[@]}-1)) ); do # choose $n random items.
        a1+=("${a[j]}")     # append to an array the chosen items.
        unset "a[j]"           # deletes the the chosen items from array.
    done
    a=("${a[@]}")               # re-build the array.
    echo "a  ${a[@]}  >> ${#a[@]}"
    echo "a1 ${a1[@]} >> ${#a1[@]}"
done

This will produce this output:

a  1 2 3 5 6 7 8 9 10 11 12 14 15 16 18 19 20 21 22 23 24 25 26 27  >> 24
a1 17 13 4 >> 3
a  1 2 5 6 7 8 10 11 12 14 15 16 18 19 20 21 22 23 24 26 27  >> 21
a1 17 13 4 25 3 9 >> 6
a  1 2 5 6 7 8 10 11 14 15 16 18 21 22 23 24 26 27  >> 18
a1 17 13 4 25 3 9 12 20 19 >> 9
a  1 2 5 7 8 10 11 14 15 16 21 22 24 26 27  >> 15
a1 17 13 4 25 3 9 12 20 19 23 6 18 >> 12
a  2 7 8 10 11 14 15 16 22 24 26 27  >> 12
a1 17 13 4 25 3 9 12 20 19 23 6 18 1 5 21 >> 15
a  2 8 10 14 15 22 24 26 27  >> 9
a1 17 13 4 25 3 9 12 20 19 23 6 18 1 5 21 7 11 16 >> 18
a  2 10 14 24 26 27  >> 6
a1 17 13 4 25 3 9 12 20 19 23 6 18 1 5 21 7 11 16 22 8 15 >> 21
a  14 26 27  >> 3
a1 17 13 4 25 3 9 12 20 19 23 6 18 1 5 21 7 11 16 22 8 15 2 24 10 >> 24
a    >> 0
a1 17 13 4 25 3 9 12 20 19 23 6 18 1 5 21 7 11 16 22 8 15 2 24 10 14 26 27 >> 27

But the same array a1 could be built just in one step with shuf:

#!/bin/bash

t=27                                # number of items in the array
n=3                                 # number of items to be chosen
a1=( $(shuf --input-range=1-"$t") )
printf '%3s ' "${a1[@]}"; echo

Which will print:

$ ./script.sh 
 15 23  1  9 24  2 21 11 12 10 19 25 27 13  5 26  4  7 14  3 22 20 17 18 16  6  8

Or, if the result must be in $n elements per line:

#!/bin/bash

t=27                                    # number of items in the array
n=3                                     # number of items to be chosen
a1=( $(shuf --input-range=1-"$t") )

while ((i+n<=t)); do
    printf '%3s ' "${a1[@]:i:n}"; echo
    ((i+=n))
done

Printing:

$ ./script.sh 
  7  19  16 
  4  20  26 
 11  23   2 
 13   6  15 
 22  12  25 
 18  14  10 
 21   8   9 
  5  24  27 
  1   3  17 

Looks like a simpler solution.


There are a few wrong things in your script, especially the way you (think you) delete the elements from the array. You're not deleting any elements, you're only replacing their values by the empty string in every field!. That's why your array has 27 elements all the way through, and after the first iteration, all the 2's and 3's are removed from each field. Here's a more idiomatic script:

#! /bin/bash

a=( {1..27} )
n=3 #number of items to be chosen
while ((${#a[@]}>=n)); do
   a1=()
   for ((i=0;i> %s\n' "${a[*]}" "${#a[@]}"
   # print chosen elements
   printf 'a1: %s >> %s\n' "${a1[*]}" "${#a1[@]}"
done

Saturday, August 18, 2018

When json_string() allocated memory is freed?

This is a stack trace of calling json_string() :

  1. json_string(const char *value)

  2. json_stringn(value, strlen(value))

  3. json_stringn_nocheck(value, len)

  4. string_create(value, len, 0)

string_create then call json_strndup(value,len) to duplicate value of string.

The problem is there that I couldn't find out when this allocated memory for value is freed.