Shop OBEX P1 Docs P2 Docs Learn Events
Looking for best way to import column of values in a TXT file into Spin program — Parallax Forums

Looking for best way to import column of values in a TXT file into Spin program

tomboardmantomboardman Posts: 68
edited 2013-05-24 20:31 in Propeller 1
I have a text file with one column of about 50 integer values and want to access the values in Spin. I would like to import the values into an array. I understand that the DAT file works with binary data, but not sure if it is adaptable for my needs. Any help would be appreciated!

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-05-24 16:59
    Yes, the file directive works only with binary data i.e. whatever is in the file is treated as such. But you could simply copy/paste the 50 values into your SPIN file and adjust them for DAT usage, e.g.
    DAT
    
    table   long    1, 2, 3, 4, 5, 6, 7, 8
            long    9, 10, ...
    
    Access would be done with e.g. v := table[4]. HTH
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-05-24 17:00
    If it's a one-off thing you should probably copy, paste, then edit manually. If the values are going to change and you have an SD card as part of your project, you could read each line and convert to a number.
  • tomboardmantomboardman Posts: 68
    edited 2013-05-24 17:18
    The 50 values will change often during initial testing of the program and don't use of SD card.

    @kuroneko, in the line, v := table[4]. HTH does v = the 4th element in the table data? What does .HTH mean? Does the table directive recognize the commas as delimiters and I can then treat the values in the order they are listed as elements of an array? I am new to Spin so please excuse my ignorance.
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-24 17:27
    ... does v = the 4th element in the table data? What does .HTH mean?
    It's in fact the 5th element (indexes start at 0). HTH: Hope That Helps
    Does the table directive recognize the commas as delimiters and I can then treat the values in the order they are listed as elements of an array?
    Exactly! FWIW, table is just a label so you can reference the DAT array from SPIN, it can be anything (except a reserved word).
  • tomboardmantomboardman Posts: 68
    edited 2013-05-24 17:30
    Great, thanks so much for the help! As you can see, I am learning both spin and forum lingo:lol:
  • pedwardpedward Posts: 1,642
    edited 2013-05-24 17:53
    You might want to look at getting a copy of SED or AWK and using that to process the file for you, with a batch file to run them.

    stuff like:

    echo "DAT" >> file.spin
    echo "table" >> file.spin
    awk '{print "\tlong\t",$1}' < data.table >>file.spin

    That would produce a file.spin that looks like:

    DAT
    table
    long 1244564
    long 2452352
    .
    .
    .
  • tomboardmantomboardman Posts: 68
    edited 2013-05-24 17:59
    Good suggestion. I ended up doing something similar but used excel to format 'long' 'value' and ',' so I can just cut and paste into spin.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-05-24 19:05
    I ended up doing something similar but used excel to format 'long' 'value' and ',' so I can just cut and paste into spin.

    You beat me to it! But since I already went to the trouble, and for the benefit of others, here are the steps:

    1. Copy the column of numbers from the text file and paste into column C of Excel.

    2. In cell A1, type the name of the array.

    3. In cell B1, type long:

    attachment.php?attachmentid=101825&d=1369447289

    4. Select cell B1, and drag the little dot down to the last row of data. This will repeat the long in every row of column B.

    attachment.php?attachmentid=101827&d=1369447290

    5. Select however many rows of columns A through C have stuff in them, and hit ctrl-C to copy it to the clipboard.

    6. Go to the PropTool, place the cursor where you want the data to appear, and hit ctrl-V to paste it:

    attachment.php?attachmentid=101826&d=1369447289

    Et voila! No nerdy Unix tools required! :)

    -Phil

    Note: For those who have not spent the money on Excel, the spreadsheet app in OpenOffice should work just as well.
    348 x 553 - 12K
    543 x 539 - 15K
    348 x 559 - 15K
  • Cluso99Cluso99 Posts: 18,069
    edited 2013-05-24 20:31
    I find excel extremely useful too :)
    You can also use formulae to combinecolumns of data, strip characters, insert, etc. Excel is extremely powerful. I expect Oppen Office has the same abilities.
Sign In or Register to comment.