|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
OP
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
I have a fair size text file (265 lines at present), which was generated by saving a spreadsheet in a comma delimited text file format (hope I got the phrasing correct, or at least understandable). Ideally, each line would have 10 items in it, however, some of them don't and using $numtok(<line>,44) is returning either 8, 9 or 10 items Here's an example of the format of the lines in the text file. First one has items missing (generating a count of 8, when using $numtok). Second one has everything in place. I think that the easiest way to handle this would be to put in something for dummy data where it's currently returning $null (and therefore not counting the number of tokens correctly) rte,address,street,ST,N,,name,account,,MTWHFS
rte,address,street,RD,S,apt,name,account,mm/dd/yyyy,MTWHFS Any suggestions as to a way that I can insert dummy data between double commas, without having to go through each line manually? There are up to three possible locations where the $null value might be getting returned...for lack of a better way of describing them, in reference to the second item in the quote, the possibilities would be at tokens numbered 5, 6 &/or 9...the rest of the locations have guaranteed entries. I know that if $numtok returns 10, then none of the three locations needs dummy data, and if it returns 7 then all three locations need the dummy data, but if it returns 8 then only 2 of the three locations need the dummy data, and if it returns 9, then one of the three needs the dummy data. I don't know how to determine (aside from visually inspecting the line), which location(s) needs the dummy data.
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
Assume %data holds the line
This well replace ,, with , , giving you a space as your dummy data %data = $replacex(%data,$+($chr(44),$chr(44)),$chr(44) $chr(44))
However might have some probs if the two $#null fields are next to each other, so i suggest the same function embeeded inside itself 3 times...
%data = $replacex([color:red]$replacex([color:orange]$replacex(%data,$+($chr(44),$chr(44)),$chr(44) $chr(44)),$+($chr(44),$chr(44)),$chr(44) $chr(44))[/color],$+($chr(44),$chr(44)),$chr(44) $chr(44))[/color]
|
|
|
|
Joined: Mar 2005
Posts: 212
Fjord artisan
|
Fjord artisan
Joined: Mar 2005
Posts: 212 |
how did you come to the conclusion that 3 times would do the job? dont get me wrong; it works i just want to know how you figured that out, if you wouldnt mind sharing
|
|
|
|
Joined: Nov 2005
Posts: 11
Pikka bird
|
Pikka bird
Joined: Nov 2005
Posts: 11 |
i would assume that since space also acts like a sperator so you need 2 on either side and the one in the middle it must assume is an actual entry anything between the both would do so i would say anything above 3 would work
|
|
|
|
Joined: Dec 2002
Posts: 1,245
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 1,245 |
Any suggestions as to a way that I can insert dummy data between double commas, without having to go through each line manually? . if the data originates from your dialog, can you have the fields fill in with default false data, such as all xxx name xxxx address xxxx phone xxxx route xxx date dd/mm/yyyy so on
|
|
|
|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
OP
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
Actually, Mike, the data isn't originating from the dialog, but will be used as the base information for the dialog. It was easier & faster to scan the information from a printout using an OCR, then save it all into one text file, using commas to separate the data on each line (couldn't use spaces, as some of the data contains spaces), rather than run my dialog and have to enter each item one at a time. Thanks for all your help.
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
how did you come to the conclusion that 3 times would do the job? dont get me wrong; it works i just want to know how you figured that out, if you wouldnt mind sharing RusselB said: "I know that if $numtok returns 10, then none of the three locations needs dummy data", he also stated this was the max that would be missing." --- I well however say that after reviewing the logic of it, I could have got away with using only 2. The worst case would be 3 in a row x,,,,y resulting in pass 1 x,dummy,,dummy,y and pass 2 x,dummy,dummy,dummy,y (dummy representing space here) I also should not have embedded the identifier as i did, this was due to forgetting repeaditive replace arguements can be passed A faster command would likely be... %data = $replace(%data,$+($chr(44),$chr(44)),$chr(44) $chr(44),$+($chr(44),$chr(44)),$chr(44) $chr(44))
|
|
|
|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
OP
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
Sorry for bring up an older topic. After testing the last code you suggested, DaveC, intermixed with code of my own, giving the following alias line.correct {
var %file = $findfile(c:\,Observer 2.csv,1), %a = 1, %b = $lines(%file)
while %a <= %b {
var %data = $read(%file,nt,%a)
if $numtok(%data,44) < 11 {
%data = $replace(%data,$+($chr(44),$chr(44)),$chr(44) $chr(44),$+($chr(44),$chr(44)),$chr(44) $chr(44))
}
.write -l $+ %a %file %data
inc %a
}
echo -a Complete
}
and then checking the lines manually, I found that the replacements are not being written. Now I admit this may be a problem with the /write command, rather than the usage of the $replace, but I don't think so. Any suggestions/recommendations?
|
|
|
|
Joined: Jan 2003
Posts: 2,523
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,523 |
The filename contains spaces, so it needs to be enclosed in quotes: var %file = $qt($findfile(c:\,Observer 2.csv,1) )Oh and don't forget to delete the file "Observer", created in the directory containing "Observer 2.csv" Or if for example the directory is "c:\program files\something\", you should look for the file "program" in c:\. You get the idea.
Last edited by qwerty; 22/06/06 12:24 AM.
|
|
|
|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
OP
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
Thanks. That solved that problem.
|
|
|
|
|