Re: Two Sort Challenges
Derek Parnell wrote:
>
> Al Getz wrote:
> >
> > Hello there,
> >
> >
> > Here's a sort challenge for anyone that's interested...
>
> Source Code: ----------------------------
> }}}
<eucode>
> include sort.e
>
> -- This routine formats the file into tuples in preparation for sorting.
> function GetData(sequence pFileName)
> -- returns sequence:Records in the format { category, detail }
> integer lFH
> sequence lRecords
> object lLine
> sequence lCategory
> integer lDetailCount
>
>
> -- Use stdin if no filename supplied.
> if length(pFileName) = 0 then
> lFH = 0
> else
> lFH = open(pFileName, "r")
> end if
>
> if lFH < 0 then
> puts(2, "Could not open file '" & pFileName & "'")
> abort(1)
> end if
>
>
> lRecords = {}
> lCategory = ""
> lDetailCount = 0
> lLine = gets(lFH)
> while sequence(lLine) do
> -- Strip off trailing LF
> if lLine[$] = '\n' then
> lLine = lLine[1..$-1]
> end if
>
> if length(lLine) = 0 then
> -- An empty line means a change of category.
> if lDetailCount = 0 and length(lCategory) > 0 then
> -- Empty category
> lRecords = append( lRecords, {lCategory, ""} )
> end if
> lCategory = ""
> elsif length(lCategory) = 0 then
> -- A non-empty line is sortable data. Its a category if there
> -- is not already a known category, otherwise its a detail.
> lCategory = lLine
> lLine = ""
> lDetailCount = 0
> end if
>
> if length(lLine) > 0 and length(lCategory) > 0 then
> -- If I have both a detail and a category then add to records
> lRecords = append( lRecords, {lCategory, lLine} )
> lDetailCount += 1
> end if
>
> lLine = gets(lFH)
> end while
>
> if lDetailCount = 0 and length(lCategory) > 0 then
> -- Empty category
> lRecords = append( lRecords, {lCategory, ""} )
> end if
>
> if length(pFileName) != 0 then
> close(lFH)
> end if
>
> return lRecords
> end function
>
> -- This routine formats the sorted data into the required format of
> -- <Category 1>
> -- <Detail 1>
> -- <Detail 2>
> -- <Detail ...>
> -- <Detail n>
> --
> -- <Category 2>
> -- <Detail 1>
> -- <Detail 2>
> -- <Detail ...>
> -- <Detail n>
>
> procedure PrintData(sequence pData, sequence pFileName)
> sequence lCategory
> integer lFH
>
> -- If no filename specified then use stdout.
> if length(pFileName) = 0 then
> lFH = 1
> else
> lFH = open(pFileName, "w")
> end if
>
> if lFH < 0 then
> puts(2, "Could not open file '" & pFileName & "'")
> abort(1)
> end if
>
>
> lCategory = ""
> for i = 1 to length(pData) do
> if equal(lCategory, pData[i][1]) = 0 then
> -- Change of category so output an empty line.
> if length(lCategory) > 0 then
> puts(lFH, '\n')
> end if
> -- Store new category and print it.
> lCategory = pData[i][1]
<snip>
Hi Derek,
Thanks, i'll try it out on my rather large data file next.
Take care,
Al
And, good luck with your Euphoria programming!
My bumper sticker: "I brake for LED's"
From "Black Knight":
"I can live with losing the good fight,
but i can not live without fighting it".
|
Not Categorized, Please Help
|
|