Source language: Translate to:

String length and arrays

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

String length and arrays

Postby dpayer » Sun Dec 31, 2006 10:11 am

I am doing text manipulation after importing a file.

To search for a sequence of characters, I convert the lines to an array and then loop through the variable array.

I use the length of each line (strlen) as the counter limit.

When done the number of elements in the array (GetArrayInfo) should equal the length of the line. I am finding that in most cases, it does but in some, it doesn't.

Here is a couple questions about strings/arrays:

1) does a string imported from a file have an end character (such as a line in a file would [#13][#10])? If so, is it counted in the strlen?
2) do arrays have any additional characters at the end of each element that note the end of the element?
3)when a file is imported line by line, does it add / remove any characters?
4) if I temporarily replace certain characters with high ascii characters, would this change the length of a string or the variable that contains the content of that string? In other words, do all characters, regardless of their ascii number take up exactly one character space in the counting of the length of a string?
5)when a string is imported, do any characters get encapsulated by NB (such as a pipe) with other characters to prevent their interacting improperly with other aspects of NB code?

I am attempting to debug something that shows more characters in a line than are in there originally and it is doing a replacement in a wrong place based on this.

Here is the section of code involved:
Code: Select all
:Go through the array's strings one by one
Loop "1" "[CSVLines]" "[CSVLoopNo]"
     StrLen "[CSVStringArray[CSVLoopNo]]" "[PresentStringLength]"
     
:If no single/beginning quotes in line, bypass arrays
SearchStr "[#174]" "[CSVStringArray[CSVLoopNo]]" "[GotQuotes]"
IfEx "[GotQuotes]=0"
GoToLine "ByPassArrays"
Else

EndIf
:Create an array of characters (ParsedString) from the string
Loop "1" "[PresentStringLength]" "[PresentStringLoopNum]"
     SubStr "[CSVStringArray[CSVLoopNo]]" "[PresentStringLoopNum]" "1" "[ParsedString[PresentStringLoopNum]]"
   
EndLoop
:TEST - make sure length of line is now same
GetArrayInfo "[parsedstring]" "" "" "[parsedstringtotal]"

AlertBox "Testing length" "originally size:[PresentStringLength] now: [parsedstringtotal] "


When I run through the list, in most cases the PresentStringLength equals the parsedstringtotal in some cases, it does not.

I have a working pub if anyone would like to look at it. I would appreciate any comments that would show me where I am not taking something into account. Thanks.

David P.
User avatar
dpayer
 
Posts: 1384
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Gaev » Sun Dec 31, 2006 11:58 am

DavidP:

May be it is because you chose to only post part of your code but I don't see ...

a) an EndLoop that matches the first (outer) Loop

b) the label :ByPassArrays

... without knowing this information, there could be a number of reasons why you are experiencing unexpected behaviour.

Also, you don't account for empty CSV lines ... i.e. "[PresentStringLength]" is zero ...

Finally, I would try checking for the non-zero values ...
Code: Select all
:If no single/beginning quotes in line, bypass arrays
SearchStr "[#174]" "[CSVStringArray[CSVLoopNo]]" "[GotQuotes]"
IfEx "[GotQuotes]>0"
Else
   GoToLine "ByPassArrays"
EndIf
... and even better, avoid using GotoLine altogether ... branching out of Loop/EndLoop blocks with GotoLine commands can confuse the NeoBook code ... once, you tell us where the ":ByPassArrays" line is supposed to be, the remaining code can be restructured to be within the If/Else/EndIf block.

But the part of your code that relates to your descriptive query ...
Code: Select all
:Create an array of characters (ParsedString) from the string
Loop "1" "[PresentStringLength]" "[PresentStringLoopNum]"
     SubStr "[CSVStringArray[CSVLoopNo]]" "[PresentStringLoopNum]" "1" "[ParsedString[PresentStringLoopNum]]"
EndLoop
:TEST - make sure length of line is now same
GetArrayInfo "[parsedstring]" "" "" "[parsedstringtotal]"

AlertBox "Testing length" "originally size:[PresentStringLength] now: [parsedstringtotal] "
... seems correct ... as long as [PresentStringLength] is not zero.

For those array items where you are seeing a discrepancy, it might be worth looking at ...

a) which value is incorrect ... [PresentStringLength] or [parsedstringtotal]

b) any common characters in the array items that are causing the discrepancy
User avatar
Gaev
 
Posts: 3738
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby dpayer » Mon Jan 01, 2007 8:51 am

Gaev wrote:DavidP:

a) an EndLoop that matches the first (outer) Loop

b) the label :ByPassArrays

Finally, I would try checking for the non-zero values

and even better, avoid using GotoLine altogether ... branching out of Loop/EndLoop blocks with GotoLine commands can confuse the NeoBook code


But the part of your code that relates to your descriptive query ...[
For those array items where you are seeing a discrepancy, it might be worth looking at ...

a) which value is incorrect ... [PresentStringLength] or [parsedstringtotal]

b) any common characters in the array items that are causing the discrepancy


Thanks!
The end loops are all there. I bypass because I define the element in the loop. If it doesn't meet the basic criteria, I bypass the analyzation process. I think a break is appropriate in this case.

I will look into zero value lines. My sample didn't have zero length lines, ut it may appear to have one for one reason or another.

RE: which values are correct. Both of them should be the same but the 2nd one is generated after a process to the string. That is why I asked about characters in strings.

Thanks for your help.

David
User avatar
dpayer
 
Posts: 1384
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Gaev » Mon Jan 01, 2007 10:20 am

David P:
RE: which values are correct. Both of them should be the same but the 2nd one is generated after a process to the string. That is why I asked about characters in strings.
... Yes, they should be the same ... but can you tell which one is NOT the correct one ? ... in other words, if the CSV line is "NeoSoft" ... which one returns 7 and which one doesn't.

I am guessing it is the "[parsedstringtotal]" that is INCORRECT ... and the reason is that you are not doing a DeleteArray between the outer Loop/EndLoop iterations ... i.e. just before starting the inner Loop ... so if your N th CSV line is (say) "Happy New Year" ... and your N+1 th CSV line is "DavidP" ... you end up with an array ...

D
a
v
i
d
P
N
e
w

Y
e
a
r

... giving you an unexpected result from the GetArrayInfo command.
User avatar
Gaev
 
Posts: 3738
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby Neosoft Support » Tue Jan 02, 2007 12:32 pm

David,

It sounds like you and Gaev are working this out, but here are some answers to your questions about strings:

1) does a string imported from a file have an end character (such as a line in a file would [#13][#10])? If so, is it counted in the strlen?


Normally, strings do not contain line break characters ([#13][#10]), but it depends on how you read the text from the file. For example, if you read each line one at a time using FileRead, your variable string will not contain any line break chars. However, if you read the entire file into a single variable using FileRead/All or FileToVar, then any line break chars contained in the file will also be present in your variable string.

2) do arrays have any additional characters at the end of each element that note the end of the element?


No.

3)when a file is imported line by line, does it add / remove any characters?


Nothing is added. Line break characters ([#13][#10]), which are used to determine how much of the file to read, are removed.

4) if I temporarily replace certain characters with high ascii characters, would this change the length of a string or the variable that contains the content of that string? In other words, do all characters, regardless of their ascii number take up exactly one character space in the counting of the length of a string?


Yes, each element in a string, regardless of the ASCII value, equals one character.

5)when a string is imported, do any characters get encapsulated by NB (such as a pipe) with other characters to prevent their interacting improperly with other aspects of NB code?


No.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5605
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA


Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 1 guest

cron