Good Question/Bad Question

Discussion in 'App Development' started by DonStar, Dec 28, 2011.

  1. moneyclip

    moneyclip

    Everything I say refers to C/C++ not to AFL directly keep that in mind!

    >So is the problem that the switch() statement does not support arrays?

    Based on the error message you get as well as the fact that you claim AFL is similar to C then yes, that is the culprit! C/C++ allows only integer types "to be switched". (Well, that's not the whole truth - if you have a class(type) for which an conversion to an integer type is defined then you could switch that one too)

    The only question is, what kind of array is the function returning (array of WHAT type) and at which location inside the array is the value you're interested in. For the fun of it I'm taking a wild guess and choose the very first location inside the array. Access to individual array values are done with the index operator[]. In C/C++ Indexing starts with 0. So in your case:

    varDayOfWeek[0] (probably) represents the first element in the array

    If the Day of the week is stored at that location and the array type is an integer type you're set and done.

    switch( varDayOfWeek[0] )
    {
    //whatever...
    }

    If for instance a character array is returned things get more complicated.

    Look at the following code:
    int MyInt = 1;
    char MyChar = '1';
    What values are stored at the memory locations where MyInt and MyChar reside ? For MyInt the value is the binary representation of 1. And for MyChar ? No, not 1 - but 49! That's the ordinal value of the character literal '1' in the ASCII table!

    So first you would have to cast (programming jargon for converting) the char to - let's say - int.
    C-style: (int) varDayOfWeek[0]
    C++-style: static_cast<int>(varDayOfWeek[0])

    Now let's take a look at the ASCII table. The digits start at location 48 (for 0) and rise to location 57 (for 9). Now in order to map the char literal '1' to the value 1 (what you would need!) how do you procede ? Well easy enough, simply subtract the ordinal value of the char literal '0' !!!! So should a char array be returned:

    switch( ((int)varDayOfWeek[0] - 48) )
    {
    //whatever...
    }

    would do the trick!

    Use the debug output window or whatever (debugger?) of Amibroker to take a look at your variable varDayOfWeek in order to get an idea what type and/or where the value you're interested is located (should varDayOfWeek[0] not hit bullseye)

    br,
    moneyclip

    PS: Before you start trading invest in your education and buy yourself an introduction level book for C/C++ You're in urgent need of one
     
    #11     Jan 7, 2012
  2. DonStar

    DonStar

    Thank you moneyclip. I can't find a "Thank you" button or I would use it.

    I really appreciate the time you take to help me.

    I will need a minute to digest this and I will get back with the results.

    Don
     
    #12     Jan 8, 2012
  3. rdg

    rdg

    If you used switch for this, it would look something like the code below. What the code attempts to do is create an array of strings (dayStr) that is set to the textual value you want by looping through the values of the dayInt array and switching on individual values. It doesn't work, however, because arrays of strings aren't supported in AFL (at least in any commonly used way).

    The power of AFL comes from its array processing capabilities. I only use loops in rare instances and only when it isn't obvious how to express something in array notation. That means that almost all AFL I write looks nothing like C. I would suggest abandoning all the C-like flow control (if/else/switch/for/etc) until you know exactly why you need it, and forcing yourself to think in array terms and using the standard AFL array operations. That may actually be easier if you don't have a background in C as it will require less rewiring.

    Code:
    // Note this code DOESN'T work:
    //
    dayInt = DayOfWeek();
    dayStr = "";
    
    for(ix = 0; ix < BarCount; ix++)
    {
      switch(dayInt[ix])
      {
         case 0:
           dayStr[ix] = "Sunday";
           break;
        case 1:
           dayStr[ix] = "Monday";
           break;
        case 2:
           dayStr[ix] = "Tuesday";
           break;
        // ...
        // case 3,4,5,6
        // ...
        default:
           dayStr[ix] = "Other";
           break;
      }
    }
    
     
    #13     Jan 8, 2012
  4. DonStar

    DonStar

    Thank you rdg. Good solid advice.
     
    #14     Jan 8, 2012