0% found this document useful (0 votes)
541 views

Read CSV Using SQR

This document contains code for reading and processing a CSV file in Square. The code defines procedures for: 1) Getting the file path of the input CSV file 2) Opening and reading from the CSV file 3) Handling fields within the file that contain embedded commas by replacing them with another character 4) Extracting and cleaning each field 5) Outputting the cleaned field values

Uploaded by

Govindarao
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
541 views

Read CSV Using SQR

This document contains code for reading and processing a CSV file in Square. The code defines procedures for: 1) Getting the file path of the input CSV file 2) Opening and reading from the CSV file 3) Handling fields within the file that contain embedded commas by replacing them with another character 4) Extracting and cleaning each field 5) Outputting the cleaned field values

Uploaded by

Govindarao
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

!

********************************************************************
! PROGRAM NAME: hcsvread.sqr
! DESCRIPTION: Read csv file(column with embeded commas)
!********************************************************************
! AUTHOR:
Govinda Rao. Balikara
! DATE:
2012 Feb 03
!********************************************************************
!********************************************************************
! Procedure: main
! Description: main Process
!********************************************************************
begin-procedure main
do main
end-program
begin-procedure main
do get_file_path
do process_data_file
end-procedure !main
!**********************************************************************
! Procedure:
get_file_path
! Description: get_file_path
!**********************************************************************
begin-procedure get_file_path
let $INPUT_FILE = 'C:\Temp\test.csv'
#ifdef debugE
show ''
show 'Full Filename: ' $INPUT_FILE
show ''
#endif
end-procedure !get_file_path
!**********************************************************************
! Procedure:
process_data_file
! Description: process_data_file
!**********************************************************************
begin-procedure process_data_file
open $INPUT_FILE as 1 for-reading record = 1000:vary status=#FILE_STATUS
if #FILE_STATUS = 0
show 'File open successful: ' $INPUT_FILE
show ''
else
show 'Error in opening file: ' $INPUT_FILE
stop
end-if
while 1
read 1 into $INPUT_RECORD:1000

if #end-file
#ifdef debugE
show 'End of File Reached '
#endif
break
end-if
#ifdef debugE
show 'Input file: ' $INPUT_RECORD
#endif
let #COUNT = 0
let #QUOTES_EXISTS = 1
let #POSITION = 1
while #COUNT < #QUOTES_EXISTS
if instr($INPUT_RECORD, '"', #POSITION) <> 0
let #QUOTES_EXISTS = 1
let #START_POINT = INSTR($INPUT_RECORD, '"',#POSITION)
let #POSITION = #START_POINT
let #END_POINT = INSTR($INPUT_RECORD, '"', #POSITION +1)
let #DIFER = #END_POINT - #START_POINT
let $STRING1 = SUBSTR($INPUT_RECORD, #START_POINT, #DIFER+1)
let $STRING2 = replace($STRING1, ',', '~')
let $INPUT_RECORD = replace($INPUT_RECORD, $STRING1, $STRING2)
else
let #QUOTES_EXISTS = 0
end-if
let #POSITION = #END_POINT + 1
end-while
unstring $INPUT_RECORD by ',' into $FIELD1 $FIELD2 $FIELD3 $FIELD4 $FIELD5 $FIEL
D6 $FIELD7 $FIELD8 $FIELD9 $FIELD10
$FIELD11 $FIELD12 $FIELD13 $FIELD14 $FIELD15 $FIELD16 $FIELD17 $FIELD18 $FIELD
19 $FIELD20
$FIELD21 $FIELD22 $FIELD23 $FIELD24 $FIELD25 $FIELD26 $FIELD27 $FIELD28 $FIELD
29 $FIELD30
$FIELD31 $FIELD32 $FIELD33 $FIELD34 $FIELD35 $FIELD36 $FIELD37 $FIELD38 $FIELD
39 $FIELD40
$FIELD41 $FIELD42 $FIELD43 $FIELD44 $FIELD45 $FIELD46 $FIELD47 $FIELD48 $FIELD
49 $FIELD50
$FIELD51 $FIELD52 $FIELD53
let
let
let
let
let
let
let
let

$FIELD1
$FIELD2
$FIELD3
$FIELD4
$FIELD5
$FIELD6
$FIELD7
$FIELD8

=
=
=
=
=
=
=
=

ltrim(rtrim(replace(replace($FIELD1,
ltrim(rtrim(replace(replace($FIELD2,
ltrim(rtrim(replace(replace($FIELD3,
ltrim(rtrim(replace(replace($FIELD4,
ltrim(rtrim(replace(replace($FIELD5,
ltrim(rtrim(replace(replace($FIELD6,
ltrim(rtrim(replace(replace($FIELD7,
ltrim(rtrim(replace(replace($FIELD8,

'~',','),
'~',','),
'~',','),
'~',','),
'~',','),
'~',','),
'~',','),
'~',','),

'"',''),'
'"',''),'
'"',''),'
'"',''),'
'"',''),'
'"',''),'
'"',''),'
'"',''),'

'),
'),
'),
'),
'),
'),
'),
'),

'
'
'
'
'
'
'
'

')
')
')
')
')
')
')
')

let
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let

$FIELD9 = ltrim(rtrim(replace(replace($FIELD9, '~',','), '"',''),' '), ' ')


$FIELD10 = ltrim(rtrim(replace(replace($FIELD10, '~',','), '"',''),' '), ' '
$FIELD11 = ltrim(rtrim(replace(replace($FIELD11, '~',','), '"',''),' '), ' '
$FIELD12 = ltrim(rtrim(replace(replace($FIELD12, '~',','), '"',''),' '), ' '
$FIELD13 = ltrim(rtrim(replace(replace($FIELD13, '~',','), '"',''),' '), ' '
$FIELD14 = ltrim(rtrim(replace(replace($FIELD14, '~',','), '"',''),' '), ' '
$FIELD15 = ltrim(rtrim(replace(replace($FIELD15, '~',','), '"',''),' '), ' '
$FIELD16 = ltrim(rtrim(replace(replace($FIELD16, '~',','), '"',''),' '), ' '
$FIELD17 = ltrim(rtrim(replace(replace($FIELD17, '~',','), '"',''),' '), ' '
$FIELD18 = ltrim(rtrim(replace(replace($FIELD18, '~',','), '"',''),' '), ' '
$FIELD19 = ltrim(rtrim(replace(replace($FIELD19, '~',','), '"',''),' '), ' '
$FIELD20 = ltrim(rtrim(replace(replace($FIELD20, '~',','), '"',''),' '), ' '
$FIELD21 = ltrim(rtrim(replace(replace($FIELD21, '~',','), '"',''),' '), ' '
$FIELD22 = ltrim(rtrim(replace(replace($FIELD22, '~',','), '"',''),' '), ' '
$FIELD23 = ltrim(rtrim(replace(replace($FIELD23, '~',','), '"',''),' '), ' '
$FIELD24 = ltrim(rtrim(replace(replace($FIELD24, '~',','), '"',''),' '), ' '
$FIELD25 = ltrim(rtrim(replace(replace($FIELD25, '~',','), '"',''),' '), ' '
$FIELD26 = ltrim(rtrim(replace(replace($FIELD26, '~',','), '"',''),' '), ' '
$FIELD27 = ltrim(rtrim(replace(replace($FIELD27, '~',','), '"',''),' '), ' '
$FIELD28 = ltrim(rtrim(replace(replace($FIELD28, '~',','), '"',''),' '), ' '
$FIELD29 = ltrim(rtrim(replace(replace($FIELD29, '~',','), '"',''),' '), ' '
$FIELD30 = ltrim(rtrim(replace(replace($FIELD30, '~',','), '"',''),' '), ' '
$FIELD31 = ltrim(rtrim(replace(replace($FIELD31, '~',','), '"',''),' '), ' '
$FIELD32 = ltrim(rtrim(replace(replace($FIELD32, '~',','), '"',''),' '), ' '
$FIELD33 = ltrim(rtrim(replace(replace($FIELD33, '~',','), '"',''),' '), ' '
$FIELD34 = ltrim(rtrim(replace(replace($FIELD34, '~',','), '"',''),' '), ' '
$FIELD35 = ltrim(rtrim(replace(replace($FIELD35, '~',','), '"',''),' '), ' '
$FIELD36 = ltrim(rtrim(replace(replace($FIELD36, '~',','), '"',''),' '), ' '
$FIELD37 = ltrim(rtrim(replace(replace($FIELD37, '~',','), '"',''),' '), ' '
$FIELD38 = ltrim(rtrim(replace(replace($FIELD38, '~',','), '"',''),' '), ' '
$FIELD39 = ltrim(rtrim(replace(replace($FIELD39, '~',','), '"',''),' '), ' '

)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)
let
)

$FIELD40 = ltrim(rtrim(replace(replace($FIELD40, '~',','), '"',''),' '), ' '


$FIELD41 = ltrim(rtrim(replace(replace($FIELD41, '~',','), '"',''),' '), ' '
$FIELD42 = ltrim(rtrim(replace(replace($FIELD42, '~',','), '"',''),' '), ' '
$FIELD43 = ltrim(rtrim(replace(replace($FIELD43, '~',','), '"',''),' '), ' '
$FIELD44 = ltrim(rtrim(replace(replace($FIELD44, '~',','), '"',''),' '), ' '
$FIELD45 = ltrim(rtrim(replace(replace($FIELD45, '~',','), '"',''),' '), ' '
$FIELD46 = ltrim(rtrim(replace(replace($FIELD46, '~',','), '"',''),' '), ' '
$FIELD47 = ltrim(rtrim(replace(replace($FIELD47, '~',','), '"',''),' '), ' '
$FIELD48 = ltrim(rtrim(replace(replace($FIELD48, '~',','), '"',''),' '), ' '
$FIELD49 = ltrim(rtrim(replace(replace($FIELD49, '~',','), '"',''),' '), ' '
$FIELD50 = ltrim(rtrim(replace(replace($FIELD50, '~',','), '"',''),' '), ' '
$FIELD51 = ltrim(rtrim(replace(replace($FIELD51, '~',','), '"',''),' '), ' '
$FIELD52 = ltrim(rtrim(replace(replace($FIELD52, '~',','), '"',''),' '), ' '

show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show
show

$FIELD53 = ltrim(rtrim(replace(replace($FIELD53, '~',','), '"',''),' '), ' '


'$FIELD1: ' $FIELD1
'$FIELD2: ' $FIELD2
'$FIELD3: ' $FIELD3
'$FIELD4: ' $FIELD4
'$FIELD5: ' $FIELD5
'$FIELD6: ' $FIELD6
'$FIELD7: ' $FIELD7
'$FIELD8: ' $FIELD8
'$FIELD9: ' $FIELD9
'$FIELD10: ' $FIELD10
'$FIELD11: ' $FIELD11
'$FIELD12: ' $FIELD12
'$FIELD13: ' $FIELD13
'$FIELD14: ' $FIELD14
'$FIELD15: ' $FIELD15
'$FIELD16: ' $FIELD16
'$FIELD17: ' $FIELD17
'$FIELD18: ' $FIELD18
'$FIELD19: ' $FIELD19
'$FIELD20: ' $FIELD20
'$FIELD21: ' $FIELD21
'$FIELD22: ' $FIELD22
'$FIELD23: ' $FIELD23
'$FIELD24: ' $FIELD24
'$FIELD25: ' $FIELD25
'$FIELD26: ' $FIELD26
'$FIELD27: ' $FIELD27
'$FIELD28: ' $FIELD28
'$FIELD29: ' $FIELD29
'$FIELD30: ' $FIELD30

show '$FIELD31:
show '$FIELD32:
show '$FIELD33:
show '$FIELD34:
show '$FIELD35:
show '$FIELD36:
show '$FIELD37:
show '$FIELD38:
show '$FIELD39:
show '$FIELD40:
show '$FIELD41:
show '$FIELD42:
show '$FIELD43:
show '$FIELD44:
show '$FIELD45:
show '$FIELD46:
show '$FIELD47:
show '$FIELD48:
show '$FIELD49:
show '$FIELD50:
show '$FIELD51:
show '$FIELD52:
show '$FIELD53:
show ''
end-while

'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'
'

$FIELD31
$FIELD32
$FIELD33
$FIELD34
$FIELD35
$FIELD36
$FIELD37
$FIELD38
$FIELD39
$FIELD40
$FIELD41
$FIELD42
$FIELD43
$FIELD44
$FIELD45
$FIELD46
$FIELD47
$FIELD48
$FIELD49
$FIELD50
$FIELD51
$FIELD52
$FIELD53

close 1
end-procedure !process_data_file

You might also like