How to loop through .txt file - coldfusion

I need help reading in a .txt file one line at a time. This is the text file for winning Powerball numbers:
Draw Date WB1 WB2 WB3 WB4 WB5 PB PP
08/27/2016 48 32 63 04 49 20 2
08/24/2016 25 11 09 65 64 16 3
I would like to read in one line at a time. The closest I got was:
<cfloop file="http://www.powerball.com/powerball/winnums-text.txt" index="chars" characters="23">
<cfoutput>#chars#</cfoutput>
<br>
</cfloop>
The output was this:
Draw Date WB1 WB2 WB3
WB4 WB5 PB PP 08/27/
2016 48 32 63 04 4
This make each 23 characters a line, which is incorrect. Also, I don't know how to get rid of the header.
Draw Date WB1 WB2 WB3 WB4 WB5 PB PP
Thank you for your time.

You need something to identify you're on the first row to not output it. Also, drop the characters="23" so the loop is based on a line breaks within the file.
<cfset outputRow = false>
<cfoutput>
<cfloop file="http://www.powerball.com/powerball/winnums-text.txt" index="chars">
<cfif outputRow>
#chars#<br>
<cfelse>
<cfset outputRow = true>
</cfif>
</cfloop>
</cfoutput>

Related

How to output query value only once?

I have query that returns four columns. One of the columns can have the same value for multiple records. I would like to output that value only once. Here is example of the data:
Rec ID Name Color Year
45 Nick Green 2018
34 Mike Red 2018
37 Nick Blue 2019
44 John Pink 2019
23 Jimmy Orange 2019
I uses this code to output the values:
<cfoutput>
<cfloop query="myQuery">
<cfif fiscal_year gt 1991>
<tr>
<td colspan="4"><a href="new_page.cfm?year=#year#>View All</a></td>
</tr>
</cfif>
<tr>
<td>#rec_id#</td>
<td>#name#</td>
<td>#color#</td>
<td>#year#</td>
</tr>
</cfloop>
</cfoutput>
My output looks like this:
View All
45 Nick Green 2018
View All
34 Mike Red 2018
View All
37 Nick Blue 2019
View All
44 John Pink 2019
View All
23 Jimmy Orange 2019
Instead I would like my output too look like this:
View All
45 Nick Green 2018
34 Mike Red 2018
View All
37 Nick Blue 2019
44 John Pink 2019
23 Jimmy Orange 2019
What is the easiest way to achieve this?
The <cfoutput> tag has a group attribute that allows you to group your query data by a column. You can nest the grouped data in another <cfoutput> tag and even group by multiple columns. It should look something like this:
<cfoutput query="myQuery" group="year">
<cfif fiscal_year gt 1991>
<tr>
<td colspan="4">View All</td>
</tr>
</cfif>
<cfoutput>
<tr>
<td>#rec_id#</td>
<td>#name#</td>
<td>#color#</td>
<td>#year#</td>
</tr>
</cfoutput>
</cfoutput>

Cfloop inside cfloop? [duplicate]

This question already has an answer here:
Why my cfloop stop after inserting first id?
(1 answer)
Closed 7 years ago.
I have a question about my cfloop inside of another cfloop. Here is my code:
<cfloop from="1" to="5" index="k">
<cfloop from="#qry.S#" to="#qry.E#" index="i" step="#CreateTimeSpan(0,0,qry.Leng,0)#">
<cfset TimeEnd = dateAdd("n", Leng, i)>
<tr>
<td>(#k#) #timeFormat(TimeStart, "hh:mm tt")# - #timeFormat(TimeEnd, "hh:mm tt")#</td>
</tr>
<cfset TimeStart = dateAdd("n", qry.Leng, i)>
</cfloop>
</cfloop>
This code above gives me output like this:
09:00 AM - 09:15 AM
09:15 AM - 09:30 AM
09:30 AM - 09:45 AM
09:45 AM - 10:00 AM
*10:00 AM - 09:15 AM
09:15 AM - 09:30 AM
09:30 AM - 09:45 AM
09:45 AM - 10:00 AM
*10:00 AM - 09:15 AM
09:15 AM - 09:30 AM
09:30 AM - 09:45 AM
09:45 AM - 10:00 AM
*10:00 AM - 09:15 AM
09:15 AM - 09:30 AM
09:30 AM - 09:45 AM
09:45 AM - 10:00 AM
*10:00 AM - 09:15 AM
09:15 AM - 09:30 AM
09:30 AM - 09:45 AM
09:45 AM - 10:00 AM
As you can see I put the star next to the line where my code gives me wrong values. For some reason my start time after first loop is done once, starts from the end time. Can anyone tell me how this can be fixed?
You can't use "i" as the index for both the outer and inner loop. Use something else for the inner loop (x). For example:
<cfloop from="1" to="5" index="i">
<cfloop from="#qry.S#" to="#qry.E#" index="x" step="#CreateTimeSpan(0,0,qry.Leng,0)#">
<cfset TimeEnd = dateAdd("n", Leng, i)>
<tr>
<td> #timeFormat(TimeStart, "hh:mm tt")# - #timeFormat(TimeEnd, "hh:mm tt")#</td>
</tr>
<cfset TimeStart = dateAdd("n", qry.Leng, i)>
</cfloop>
</cfloop>
I'm not sure of the intention here so you may need to swap some of you i's for x's in the inner loop depending on what you are after (start-end for example).
*************************** edits ****************
Maybe I see your issue (not sure) but based on your comment your issue is going to be that you have reset the timestart var. You need to do that after your first loop begins.
<cfloop> outer loop
<Cfset timestart = *starting value*>
<cfloop> inner looop
Other wise your timestart is going to be whatever your last cfset for it was - in the inner loop.

Check if a column exists and then sum in SAS

This is my input dataset:
Ref Col_A0 Col_01 Col_02 Col_aa Col_03 Col_04 Col_bb
NYC 10 0 44 55 66 34 44
CHG 90 55 4 33 22 34 23
TAR 10 8 0 25 65 88 22
I need to calculate the % of Col_A0 for a specific reference.
For example % col_A0 would be calculated as
10/(10+0+44+55+66+34+44)=.0395 i.e. 3.95%
So my output should be
Ref %Col_A0 %Rest
NYC 3.95% 96.05%
CHG 34.48% 65.52%
TAR 4.58% 95.42%
I can do this part but the issue is column variables.
Col_A0 and Ref are fixed columns so they will be there in the input every time. But the other columns won't be there. And there can be some additional columns too like Col_10, col_11 till col_30 and col_cc till col_zz.
For example the input data set in some scenarios can be just:
Ref Col_A0 Col_01 Col_02 Col_aa Col_03
NYC 10 0 44 55 66
CHG 90 55 4 33 22
TAR 10 8 0 25 65
So is there a way I can write a SAS code which checks to see if the column exists or not. Or if there is any other better way to do it.
This is my current SAS code written in Enterprise Guide.
PROC SQL;
CREATE TABLE output123 AS
select
ref,
(col_A0/(Sum(Col_A0,Col_01,Col_02,Col_aa,Col_03,Col_04,Col_bb)) FORMAT=PERCENT8.2 AS PERCNT_ColA0,
(1-(col_A0/(Sum(Col_A0,Col_01,Col_02,Col_aa,Col_03,Col_04,Col_bb))) FORMAT=PERCENT8.2 AS PERCNT_Rest
From Input123;
quit;
Scenarios where all the columns are not there I get an error. And if there are additional columns then I miss those. Please advice.
Thanks
I would not use SQL, but would use regular datastep.
data want;
set have;
a0_prop = col_a0/sum(of _numeric_);
run;
If you wanted to do this in SQL, the easiest way is to keep (or transform) the dataset in vertical format, ie, each variable a separate row per ID. Then you don't need to know how many variables there are to figure it out.
If you always want to sum all the numeric columns then just do :
col_A0 / sum(of _numeric_)

access read memo field

I have an access database that has a table with a memo field. Fields have been inserted in this format.
Apr 02 - some text
Feb 20 - some text
I would like to reverse the order of the inserts so the above would be:
Feb 20 - some text
Apr 02 - some text
I am thinking of reading line by line using regular expressions, anyone has a better path to achieve that
Your memo field contains 2 lines of text and you want to reverse their order. You can do that with a simple VBA procedure, which doesn't need a regular expression.
Here is a sample Immediate window session which demonstrates techniques you can use in a VBA procedure.
MyText = "Apr 02 - some text" & vbcrlf & "Feb 20 - some text"
? MyText
Apr 02 - some text
Feb 20 - some text
? Split(MyText, vbcrlf)(1)
Feb 20 - some text
? Split(MyText, vbcrlf)(0)
Apr 02 - some text
If the memo field can include more than two lines of text, you can load an array with the results from Split() and then loop through the array in reverse order.

ColdFusion - Displaying rows as columns

I'm building a scorecard with ColdFusion which will allow for updating data. I need to display the data with the rows as columns and columns as rows. Here is an example of the table:
Mon Met1 Met2 Met3 Met2 Met5
Jan 15 24 21 40 50
Feb 30 21 14 39 44
Mar 15 19 20 28 19
Apr 16 31 33 21 43
I want to display the data (flipping the rows and columns) like this:
Metric Jan Feb Mar Apr
Met1 15 30 15 16
Met2 24 21 19 31
Met3 21 14 20 33
Met4 40 39 28 21
Met5 50 44 19 43
Any help would be appreciated.
Thanks
Sounds like a 'vertical sorting' FAQ I wrote years ago. Perhaps this will help:
<html>
<head>
<title>Vertical Sorting</title>
</head>
<cfquery name = "qMyQuery" datasource = "dsn">
SELECT fields
FROM table
ORDER BY myField
</cfquery>
<body>
<!--- set the number of colums you wish to have --->
<cfset cols = 5>
<!--- get the number of rows so you know what record to display at the top of the next row. for example if our query contains "a,b,c,d,e,f,g,h,i,j,k,l,m" (13 elements) it will produce 3 totalrows--->
<cfset totalRows = ceiling(qMyQuery.RecordCount / cols)>
<!--- set inital record to 1 "output" is the actual cell of the query --->
<cfset output = 1>
<!--- Create table --->
<table width = "100%" border="0" align="center" cellpadding="2" cellspacing = "2">
<!--- loop through the rows. This loop will run 3 times in this example --->
<cfloop from = "1" to = "#totalRows#" index = "thisRow">
<tr>
<!--- this loop will run 5 times in times in this example --->
<cfloop from = "1" to = "#cols#" index = "thisCol">
<!--- the width in the table cell will dynamicaly calculated to evenly distribute the cells. in this example if cols = 5 100/5 will make the cells 20% of the table --->
<td width = "<cfoutput>#numberformat((100/cols), 99)#</cfoutput>%" align="center" nowrap style = "border: 1px solid #ccc;">
<!--- Check current record with the record count, this will be used to display data or an empty cell --->
<cfif output lte qMyQuery.recordCount>
<cfoutput>#qMyQuery.Mon[output]#</cfoutput>
<cfelse>
<!--- use <br> to display an empty cell --->
<br>
</cfif>
<!--- increment counter to the next record in this example if we started on the first cell of the first row it would be 1(a), then 4(d), then 7(g) and so on if this was the firs cell on the second row it would be 2(b), 5(e), 8(h), continue... --->
<cfset output = output + totalRows>
</td>
</cfloop>
<!--- this little bit tells where to start the next row. if we just finished the first row output would be 2(b) --->
<cfset output = thisRow + 1>
</tr>
</cfloop>
</table>
</body>
</html>