2

I have a string passed into a function, I would like to compare the first character of the string against a number.

I.E. if String(1) = "3" then 

When I compile I get:

warning: index for String may assume lower bound of 1 warning: suggested replacement String'First + 1 

I would really like to make this right, but when I try "first" it actually grabs a number, not the character.

Is there a better way to do it?

I tried looking up the 'First concept, and the below site explains I'm actually getting the number of the index, not the actual contents: http://en.wikibooks.org/wiki/Ada_Programming/Types/array

For example,

Hello_World : constant String := "Hello World!"; World : constant String := Hello_World (7 .. 11); Empty_String : constant String := ""; 

Using 'First I'll get:

Array 'First 'Last 'Length 'Range Hello_World 1 12 12 1 .. 12 World 7 11 5 7 .. 11 Empty_String 1 0 0 1 .. 0 

Based on that information, I can't get H from Hello world (for a comparison like if Hello_World(1) = "H" then)

EDIT: So the way I initially was doing it was (insert some variable name instead of string in this case)

String(String'First .. String'First) = "1" 

So that works from what I can tell, however, rather then writing all that, I found out that

String(String'First) = '1' 

Does the same thing but using char comparison, which makes a lot more sense!

Thanks for all the answers everyone!

3 Answers 3

3

Strings are the biggest bugaboo for newbie Ada coders; particularly so for those who are already experts at dealing with strings in Cish languages.

Ada strings (in fact all Ada arrays) are not 0 based like C, or 1-based like Fortran. They are based however the coder felt like it. If someone wants to index their string from 10 ... 200, they can. So really the safest way to acces characters in an Ada string is to use the 'first attribute (or better yet, loop through them using 'range or 'first .. 'last).

In your case it looks like you want to get at only the first character in the string. The easiest and safest way to do that for a string named X is X(X'first).

In pactice you would almost never do that though. Instead you would be looping through the string's 'first...'last looking for something, or just using one of the routines in Ada.Strings.Fixed.

Sign up to request clarification or add additional context in comments.

3 Comments

Er... Y'know, you can't actually have a Standard.String with a -200 .. -10 index range because of the unconstrained Positive Index subtype. Well, at least not one you can do anything significant with :-)
Hmmm. It seems to me I saw that once back in the ancient days, but I'll fix my answer to use positive numbers just to be safe. (Perhaps it was an unconstrained array of characters instead, or some other unconstrained array, and my memory's failing early).
You can declare such a string--with negative index(es), and it will compile, though with warnings. But don't try and execute it, unless of course you're testing for Constraint_Error exception handling :-)
2

The warning is suggesting you use:

String(String'First + Index) 

Instead of just

String(Index) 

4 Comments

I just discussed this with another engineer, and pretty much got the same answer.
That does appear to be what it's suggesting, but that's not going to work. See my answer below...
Agreed. What you really want to do here is use something like 'first instead of 1
And that suggestion is wrong. You want String(String'First + Index - 1) .
1

There's something odd about the code in your question. First off, that you're calling your variable "String" and that it's of type "String". Ada will balk at that right off the bat.

And the warning statements you reproduce for that code fragment don't make sense.

Let's say your variable is actually called "Value", i.e.:

Value : String := "34543"; 

Value(1) is not the same as Value(Value'First + 1), because Value'First (in this declaration) is 1. So you end up referencing Value(1 + 1). You appear to be experiencing this because of mentioning that you can't reference the 'H' in a "Hello World" string.

Now the warning is valid, in that you're safer using 'First (and 'Last and 'Range) to reference array bounds. But you need to use the proper indexing if you're going to offset from the bound retrieved via 'First, typically using either 0-based or 1-based (in which case you need to offset by 1). Use whichever base is more appropriate and readable in your context.

2 Comments

Marc, I don't understand "in this case either 0-based, or 1-based (in which case you need to offset by 1)"? Can't be 0-based (as you remarked to T.E.D); and why would I offset by 1 if the string was 1-based?
Well, it can be a 0-based index if you're adding it to the 'First value. And I know that "1-based (and offset by 1)" is sorta counter-productive, but it might make sense in certain contexts. The optimizer should sort that out. Mmm, I'll go edit it and try to make this all a little clearer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.