Shop OBEX P1 Docs P2 Docs Learn Events
combining variables — Parallax Forums

combining variables

ThricThric Posts: 109
edited 2010-06-27 14:27 in Propeller 1
Hi,
I'm trying to create a program that finds a single characther in a txt file that·is located on a SD card and then stores that value into an array. However when a certain charcheter is reached (like a comma) then I need to combine all my previous values so that I end up with a number. Heres an example:

buf[noparse][[/noparse]1]:=5, buf[noparse][[/noparse]2]:=8, and buf[noparse][[/noparse]3]:=","

then I need to some how store buf[noparse][[/noparse]1] and buf[noparse][[/noparse]2] into a single variable to equal 58.

·Sorry if its a little confusing, its hard for me to explain something I don't know how to do confused.gif .


Thanks

Comments

  • MacGeek117MacGeek117 Posts: 747
    edited 2010-06-26 14:39
    Hey, Thric! Welcome to the Forums!
    All you Prop pros out there correct me if I'm wrong on this, but I believe you can do something like this:
    x := (buf * 10) + buf
    MG117

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    BS2: $49


    SX48 Protoboard: $10

    Propeller Protoboard: Priceless

    www.apple.com

    www.parallax.com

    www.goldmine-elec.com

    www.expresspcb.com

    www.jameco.com
  • ThricThric Posts: 109
    edited 2010-06-27 03:11
    Thanks for the reply,
    · will this work with the following example?

    buf[noparse][[/noparse]1]:=2·· buf[noparse][[/noparse]2]:=3 ·buf[noparse][[/noparse]3]:=7 ·buf[noparse][[/noparse]4]:="."· buf[noparse][[/noparse]5]:=9 ·buf[noparse][[/noparse]6]:=","

    So the outcome will have to be 237.9



    Post Edited (Thric) : 6/27/2010 3:17:21 AM GMT
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-06-27 04:29
    If you want to print the character 2 buf := 2 + 48 because the ascii code for 2 is 50 (decimal). Take a look at Simple_Numbers.spin in the library (where the propeller.exe file is located)

    John Abshier
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-06-27 09:25
    one thing to clear up is:

    do you want the numbersmfor calculations inside the prop or are you just displaying them?

    In the commandset of the SPIN-interpreter you can only calculate with INTEGERs.
    For floating point you would have to use floatingpoint math-object

    If you want the numbers only to DISPLAY with really really NO calculations you can do this

    buf[noparse][[/noparse] 1 ]:=2 buf[noparse][[/noparse] 2 ]:=3 buf[noparse][[/noparse] 3 ]:=7 buf[noparse][[/noparse] 4 ]:="." buf[noparse][[/noparse] 5 ]:=9 buf[noparse][[/noparse] 6 ]:=","

    defining a second array of bytes

    CON
      _xinfreq = 5_000_000       'chrystal-frequency                      
      _clkmode = xtal1 + pll16x  'clockmode
    
    OBJ
      Debug : "FullDuplexSerial"
    
    
    VAR
      long Index
      byte buf[noparse][[/noparse] 7 ]
      byte MyCharArray[noparse][[/noparse] 7 ]
                       
    
    PUB Main
    
      Debug.Start(31,30,0,115200) 'start cog with serial driver
      Debug.Str(string("Debug-output started",13))
    
      buf[noparse][[/noparse] 1 ] := "2"
      buf[noparse][[/noparse] 2 ] := "3"
      buf[noparse][[/noparse] 3 ] := "7"
      buf[noparse][[/noparse] 4 ] := "."
      buf[noparse][[/noparse] 5 ] := "9"
      buf[noparse][[/noparse] 6 ] := ","
    
      repeat index from 1 to 5
        MyCharArray[noparse][[/noparse] Index ] := buf[noparse][[/noparse] Index ]
    
      MyCharArray[noparse][[/noparse] 6 ] := 0     
    
      Debug.Str(string("right before output",13))
      
      Debug.Str(@MyCharArray[noparse][[/noparse] 1 ])
      Debug.Tx(13)
      
      Debug.Str(string("Output done",13))
    
    



    remember as soon as you want to do any kind of calculations you have to convert the characters into a real integer

    how this works depends on the format how your numbers are stored on the SD-card
    the above example will work if the numbers are stored as ASCII-codes characters

    so next step for you is to post an answer with more information of how the values are stored and what you want to to with the values

    best regards

    Stefan
  • ThricThric Posts: 109
    edited 2010-06-27 14:04
    Thanks for all the replys,
    The informations is stored on the sd card in a txt file as ASCII. What I want to do is take each character into a a buffer till a comma is found then combine those numbers and do some further calculations with them. the object I'm using for my sd card is the Fat 16 routines by john Twomey (http://obex.parallax.com/objects/402/) if this helps at all.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-06-27 14:27
    What you need is a method to convert your string characters to a value -- like this:

    pub str2dec(pntr) | val, c
    
      val := 0                                                      ' clear workspace
    
      repeat
        c := byte[noparse][[/noparse]pntr++]                                           ' get character, point to next
        if (c => "0") and (c =< "9")                                ' digit?
          val := (val * 10) + (c - "0")                             ' yes, add to value
        else
          quit                                                      ' no, we're done
    
      return val
    


    To use it you will pass a pointer to the first character of the string you want to convert, for example:

    value := str2dec(@buf[noparse][[/noparse] 1 ])
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
Sign In or Register to comment.