* comparing characters in string
@ 2014-10-04 23:03 Stribor40
2014-10-05 1:41 ` Shark8
0 siblings, 1 reply; 5+ messages in thread
From: Stribor40 @ 2014-10-04 23:03 UTC (permalink / raw)
I am trying to do something with this function...For example I would pass 2 strings to function like this....
function tt (a : in String ; b : in String) return Boolean is
begin
for i in a'Range loop
if a'(a'First) = b(b'First + 1) then
return false;
end if;
end loop;
return true;
end tt;
I am calling it like so...
b: Boolean := tt("123","321");
What the function is suppose to do is take first character from first parametar and compare it to every character in string b but not first character of string b.
Unfortunately my compiler doesnt like me and gives me this error...
subtype mark required in this context
found "a" declared at line 6
compilation error
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: comparing characters in string
2014-10-04 23:03 comparing characters in string Stribor40
@ 2014-10-05 1:41 ` Shark8
2014-10-05 7:06 ` Simon Wright
0 siblings, 1 reply; 5+ messages in thread
From: Shark8 @ 2014-10-05 1:41 UTC (permalink / raw)
On 10/4/2014 4:03 PM, Stribor40 wrote:
> if a'(a'First) = b(b'First + 1) then
You're using qualification in that if, which is a semantic error.
Qualification is a method to explicitly tell the compiler what type
something is, consider having the following functions:
Function K return String;
Function K return Character;
Function J( I : Character ) return String;
Function J( I : String ) return String;
and the following declaration:
Data : String := J(K);
Which versions of J and K are used?
Well, it's ambiguous, so the compiler is to reject the compilation with
an error; but if we had some way to explicitly tell the compiler what
type K returns then we would resolve the ambiguity.
That is exactly what qualification is/does. Qualification is done with
TYPE_NAME'( expression ), so we would resolve the above as follows:
Data : String := J( Character'(K) ); -- Forces the K returning a
character.
If you delete that tick after a, it should compile.
if a(a'First) = b(b'First + 1) then
But there's a problem with your logic:
a'(a'first) and b(b'first + 1) are both constant, so your loop is
meaningless.
Try:
function tt (a, b : in String) return Boolean is
Target : Character renames a(a'first);
Found : Constant Boolean:= True;
Substring : constant String:= b(b'first+1..b'last);
begin
for Index in substring'range loop
if target = Substring(Index) then
return Found;
end if;
end loop;
return not Found;
end tt;
Or, if you're doing Ada 2012:
function tt (a, b : in String) return Boolean is
Target : Character renames a(a'first);
Found : Constant Boolean:= True;
Substring : constant String:= b(b'first+1..b'last);
begin
return (if (for some Ch of Substring => Ch = Target) then Found
else not Found);
end tt;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: comparing characters in string
2014-10-05 1:41 ` Shark8
@ 2014-10-05 7:06 ` Simon Wright
2014-10-05 12:16 ` Shark8
0 siblings, 1 reply; 5+ messages in thread
From: Simon Wright @ 2014-10-05 7:06 UTC (permalink / raw)
Shark8 <OneWingedShark@gmail.com> writes:
> function tt (a, b : in String) return Boolean is
> Target : Character renames a(a'first);
> Found : Constant Boolean:= True;
> Substring : constant String:= b(b'first+1..b'last);
> begin
> return (if (for some Ch of Substring => Ch = Target) then Found
> else not Found);
> end tt;
Couldn't that just be
function tt (a, b : in String) return Boolean is
Target : Character renames a(a'first);
Substring : constant String:= b(b'first+1..b'last);
begin
return (for some Ch of Substring => Ch = Target);
end tt;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: comparing characters in string
2014-10-05 7:06 ` Simon Wright
@ 2014-10-05 12:16 ` Shark8
2014-10-05 12:41 ` Simon Wright
0 siblings, 1 reply; 5+ messages in thread
From: Shark8 @ 2014-10-05 12:16 UTC (permalink / raw)
On 10/5/2014 12:06 AM, Simon Wright wrote:
> Shark8 <OneWingedShark@gmail.com> writes:
>
>> function tt (a, b : in String) return Boolean is
>> Target : Character renames a(a'first);
>> Found : Constant Boolean:= True;
>> Substring : constant String:= b(b'first+1..b'last);
>> begin
>> return (if (for some Ch of Substring => Ch = Target) then Found
>> else not Found);
>> end tt;
>
> Couldn't that just be
>
> function tt (a, b : in String) return Boolean is
> Target : Character renames a(a'first);
> Substring : constant String:= b(b'first+1..b'last);
> begin
> return (for some Ch of Substring => Ch = Target);
> end tt;
>
Yes, but in the original posted code the return-value was false if
found, true if not.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: comparing characters in string
2014-10-05 12:16 ` Shark8
@ 2014-10-05 12:41 ` Simon Wright
0 siblings, 0 replies; 5+ messages in thread
From: Simon Wright @ 2014-10-05 12:41 UTC (permalink / raw)
Shark8 <OneWingedShark@gmail.com> writes:
> On 10/5/2014 12:06 AM, Simon Wright wrote:
>> Shark8 <OneWingedShark@gmail.com> writes:
>>
>>> function tt (a, b : in String) return Boolean is
>>> Target : Character renames a(a'first);
>>> Found : Constant Boolean:= True;
>>> Substring : constant String:= b(b'first+1..b'last);
>>> begin
>>> return (if (for some Ch of Substring => Ch = Target) then Found
>>> else not Found);
>>> end tt;
>>
>> Couldn't that just be
>>
>> function tt (a, b : in String) return Boolean is
>> Target : Character renames a(a'first);
>> Substring : constant String:= b(b'first+1..b'last);
>> begin
>> return (for some Ch of Substring => Ch = Target);
>> end tt;
>>
>
> Yes, but in the original posted code the return-value was false if
> found, true if not.
Then both of us are wrong!
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-10-05 12:41 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-04 23:03 comparing characters in string Stribor40
2014-10-05 1:41 ` Shark8
2014-10-05 7:06 ` Simon Wright
2014-10-05 12:16 ` Shark8
2014-10-05 12:41 ` Simon Wright
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox