Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Array of String - check for existence of item
Wed, Sep 17 2014 8:10 AMPermanent Link

robdev

Paperless Innovation Ltd

Avatar

Hi

I have Delphi/EWB code as follows

var
  MyCompany: String;
  Response: Array of String;
....
SetLength(Response,2);
Response['CA'] := 'Company name A';
Response['CB'] := 'Company name B';
....
MyCompany := Response['CA'];

The above code works without issue

But I want to do the following, question is how?

1. Detect if an element exists in an array. This is the same as "isset" in php (for those who know php)

if function_name(Response['CA']) then
begin
....
end;

2. Loop through all items in the array, but not using an integer index. I would like to do something like...

var
  Index: String;
begin
  for Index in Response do
  begin
     messagedlg(Index + ' = ' + Response[Index], mtinformation, [mbok], 0);
  end;
end;

Obviously I want to do something more meaningful than a 'messagedlg' !

Is this possible with EWB? If not any alternative code structures which would help.
Wed, Sep 17 2014 8:17 AMPermanent Link

Matthew Jones

robdev wrote:

>    for Index in Response do

Can you do

  for Index from 0 to Length(Response) - 1 do

I presume that would work (might want to check the start/end).

--

Matthew Jones
Wed, Sep 17 2014 9:25 AMPermanent Link

robdev

Paperless Innovation Ltd

Avatar

Thanks for suggestion. No joy. That generates

"Expected : but instead found from" error

---

"Matthew Jones" wrote:

robdev wrote:

>    for Index in Response do

Can you do

  for Index from 0 to Length(Response) - 1 do

I presume that would work (might want to check the start/end).

--

Matthew Jones
Wed, Sep 17 2014 12:25 PMPermanent Link

D.C.

This may help:

1. Check against nil:

if not Response['CA'] = nil then ...


2. You may use TStrings instead of arrays

var
Response: TStringList;
n: Integer;
begin                 
  Response:=TStringList.Create;
  Response.Add('CA=Company name A');
  Response.Add('CB=Company name B');
  for n:=0 to Response.Count-1 do
  begin
     ShowMessage(Response.Names[n]);  //this will show 'CA' and 'CB'
  end;


TStrings have a great feature, you may asign names instead of indexes using MyList.Add('TheName=TheContent') and then call MyList['TheName'] and get 'TheContent'. Besides that, you have access to the list of names using the index.

The nil comparison will work also with TStrings

Regards
Diego
Thu, Sep 18 2014 4:42 AMPermanent Link

robdev

Paperless Innovation Ltd

Avatar

Thanks Diego

Useful info
Thu, Sep 18 2014 5:02 AMPermanent Link

Matthew Jones

robdev wrote:

> Thanks for suggestion. No joy. That generates
>
> "Expected : but instead found from" error
>
> ---
>
> "Matthew Jones" wrote:
>
> robdev wrote:
>
> >    for Index in Response do
>
> Can you do
>
>    for Index from 0 to Length(Response) - 1 do
>
> I presume that would work (might want to check the start/end).


For completeness, I gave it a whirl and it can compile like this:

   for nIndex := 0 to Length(Response) - 1 do
   begin
       memo1.Lines.Add(Response.[nIndex]);
   end;

However it doesn't give the result sought as the 0 to N is not the
indexes that are in use...

I suggest that this would be a handy language feature to have,
particularly for interaction with other libraries.

http://stackoverflow.com/questions/9329446/how-to-do-for-each-over-an-ar
ray-in-javascript suggests various ways to do this in javascript, but I
see no "for in" in the output. One for 2.1 I suggest.


--

Matthew Jones
Image