Page 1 of 1 1
Topic Options
#212315 - 2017-03-02 11:09 PM Parsing 2 files selecting output
Sweeny Offline
Fresh Scripter

Registered: 2016-03-18
Posts: 18
Loc: Hampshire
I'm having a hard time trying to get the concept of a bit of code to work. Presently I have 2 files, for the sake of ease, File A and File B. File A contains headings then information below in regards to the heading, for example:

1,a heading, some more information
Stuff stuff stuff
Some more stuff more stuff
2, a second heading, even more information
Stuff stuff

File B contains lines in pairs, one line of the pair is the headings found in File A, the second line is information linked to the first.
I have written (in hast mind you) a small script to search File A and B. If the script finds a matching heading in both files I'd like it to write out the pair of accossiated lines in File B followed by the accossiated information in File A, only write out the lines in File A up until the next heading. This should loop through until the end of File A is reached.
Not having had much success I ask for some help with this approach
Many thanks in advance,
Tom

Top
#212316 - 2017-03-02 11:58 PM Re: Parsing 2 files selecting output [Re: Sweeny]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Here's some pseudo-ish code, just whipped together/untested:
 Code:
; Load both files into arrays
$aFileA = FileIO('fileA.ext', 'R')
$aFileB = FileIO('fileB.ext', 'R')

; Enumerate File A
For Each $Line in $aFileA
  ; Detect when the line is a header - look for something that identifies a header
  If InStr($Line, "header stuff:") ; found a header
     ; might need to split off the header part?
     $Header = Split($Line, ':')[0] ; assumes that the headers looks like "1, Header stuff: other stuff...) - take everything up to the ':'

     ;Look in the other file for a line containing the same header
     $Target = AScan($aFileB, $Header) ; check args here - might want a partial match
     If $Target <> -1 ; was found!
       ; $Target is the line number in File B's array - read that and extract data until the next header is found
       For $P = $Target to $Target + 999 ; scan next 999 lines or till we find the next header
         If $P > $Target And InStr($aFileB[$P], 'Header stuff') ; found the NEXT header
           $P = $Target + 1000 ; terminate the loop
         Else
           $aFileB[$P] ? ; output for testing, manipulate if necessary
           ; This line from File B has data for a matching header. You may need to 
           ; split the File B header ($P = $Target means this is the header line in File B)
         EndIf
       Next
     EndIf
  EndIf
Next
The FileIO reads a file into an array, can also write an array to a file. Download from the UDF library on my web site (see sig).

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#212324 - 2017-03-08 03:19 PM Re: Parsing 2 files selecting output [Re: Glenn Barnas]
Sweeny Offline
Fresh Scripter

Registered: 2016-03-18
Posts: 18
Loc: Hampshire
Glenn that is perfect, after a little bit of bug testing and tweaking its working without a hitch.
Cheers,
Tom \:\)

Top
#212328 - 2017-03-10 04:18 AM Re: Parsing 2 files selecting output [Re: Sweeny]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Thanks for following up! Glad you found it helpful.

Post your final code if you don't mind sharing..

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#212359 - 2017-03-22 11:35 AM Re: Parsing 2 files selecting output [Re: Glenn Barnas]
Sweeny Offline
Fresh Scripter

Registered: 2016-03-18
Posts: 18
Loc: Hampshire
Hi Glenn,
Here is a snippet of my code where it was used.
 Code:
For Each $Line in $aFileA
		If InStr($Line, ",Name,")
			writeline($handle2,@crlf + $Line)
		endif
		Select
			Case InStr($Line, "wibble") <> 0			
				$Target = AScan($Catfiltarray, $Line,,,1) ; Argument tells script to look for partial match
				If $Target <> -1 					; was found!
													; $Target is the line number in File B's array - read that and extract data until the next header is found
					For $P = $Target to $Target + 999 ; scan next 999 lines or till we find the next header
						If $P > $Target And InStr($Catfiltarray[$P], ',Name,') ; found the NEXT header
							$P = $Target + 1000 						; terminate the loop
						Else
							Select 
								case InStr(substr($Catfiltarray[$P],2,9), "Category")				;searching for a line conatining the string Category
									$Catfiltarray[$P] = trim(substr($Catfiltarray[$P], 10))					;some sorting to make results more readable
									$Catfiltarray[$P + 1] = trim(substr($Catfiltarray[$P + 1], 9))
									writeline($handle2,$Catfiltarray[$P] + "," + $Catfiltarray[$P + 1] + @crlf)
								case InStr($Catfiltarray[$P], ",Name,")
									$Catfiltarray[$P] = trim(substr($Catfiltarray[$P], 8))
									$comma = trim(substr($Catfiltarray[$P],1,1))
									if $comma = ","										;If statement compensating for more than 10 catagories present in websense 
										$Catfiltarray[$P] = trim(substr($Catfiltarray[$P], 9))		;Will start trimming from 9 characters instead of 8, compensate for 2 character numbers					
									endif
									writeline($handle2,@crlf + ",Category - ," + $Catfiltarray[$P] + @crlf)
							EndSelect
						EndIf
					Next
				EndIf
			Case InStr($Line, "wibble2") <> 0			
				$Target = AScan($limfilarray, $Line,,,1) ; Argument tells script to look for partial match
				If $Target <> -1 					
					For $P = $Target to $Target + 999 ; scan next 999 lines or till we find the next header
						If $P > $Target And InStr($limfilarray[$P], ',,') 
							$x = writeline($handle2,@crlf)
							$P = $Target + 1000 						
						Else
							If Instr($limfilarray[$P], "http")
								$x = writeline($handle2,@crlf + ",Filter" + $limfilarray[$P])
							endif
						EndIf
					Next
				EndIf
		EndSelect
	Next

Please forgive any bad practices it was a rather time constrained write!
Cheers,
Tom

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 557 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.033 seconds in which 0.013 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org