Menu

Type coercion in Unicon style

Anonymous
2017-12-28
2017-12-29
  • Anonymous

    Anonymous - 2017-12-28

    Newbie question...

    I'm reading a CSV file, and want to pull a column out to sort, but in order to sort it (I think) I should be storing it as integer.

    The code (without coercion) is:

            entryPricePositions := []
    
            every put(entryPricePositions, (ParseLine(!entries, ","))[9])
    
            entryPricePositionsSorted := sort(entryPricePositions)
    

    where entries consists of lines of CSV values and ParseLine splits columns by a comma. The 9th column is the one I want to sort.

    I'm unsure if there's an elegant way to do this using Unicon iteration, or if I need to manually kludge it?

     

    Last edit: Anonymous 2017-12-28
    • Jafar

      Jafar - 2017-12-28

      As far as I remember, sort() knows how to sort strings, so you should be good to go. Did you try this and it didn't work?

       
      • Anonymous

        Anonymous - 2017-12-28

        Hi Jafar,
        Yes, in this case the column contains variable-length numbers, and the sort appears to left-justify the field; e.g. the result will contain something like:
        1
        10
        11
        2
        3
        ...

         
    • Bruce Rennie

      Bruce Rennie - 2017-12-29

      Good afternoon,

      On the line that you as below
      On 29/12/17 01:54, noreply@in.sf.net wrote:

           every put(entryPricePositions, (ParseLine(!entries, ","))[9])
      

      change it to

           every put(entryPricePositions, integer((ParseLine(!entries, ","))[9]))
      

      This will change the strings to integers and sort will sort on the
      integer value

      regards

      Bruce Rennie

       
      • Anonymous

        Anonymous - 2017-12-29

        Thanks, Bruce.

        I'd been trying :integer everywhere with no luck...!

         

Anonymous
Anonymous

Add attachments
Cancel