We help IT Professionals succeed at work.

Does this subroutine delete every string?

John Ikea
John Ikea asked
on
189 Views
Last Modified: 2017-05-16
I have a text file which the user enters names into separated by paragraphs e.g:
John
Amy
Theo

These are then loaded into a stringlist and sorted but before they are sorted alphabetically I want it to check each name for invalid characters e.g integers. I use the following code but I believe it deletes everything or stops after one name:

for StringlistNum := 0 to StringList.count-1 do
    begin
        CheckName := StringList[StringListNum];
        for NameChar := 0 to (Length(CheckName)-1) do
         begin
          if not (CheckName[NameChar] in Chars) then
           ShowMessage(CheckName+' has been removed. Names must only contains letters');        //Needs fixing
           StringList.Delete(StringListNum);                                                    //Either deletes everything
         end;                                                                                   //Or doesn't run code after
    end; 

Open in new window


Chars is a constant including  ['A'..'Z', 'a'..'z']
Comment
Watch Question

It seems so although I am not en expert.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Hi,

pls try
for StringlistNum := 0 to StringList.count-1 do
    begin
        CheckName := StringList[StringListNum];
        for NameChar := 0 to (Length(CheckName)-1) do
         begin
          if not (CheckName[NameChar] in Chars) then
           ShowMessage(CheckName+' has been removed. Names must only contains letters');        //Needs fixing
           StringList.Delete(StringListNum);
           Break;                                                    //Either deletes everything
         end;                                                                                   //Or doesn't run code after
    end; 

Open in new window

Regards

Author

Commented:
After the subroutine I need it to do the following:

StringList.sort;                   //Built in quicksort
    StringList.saveToFile(OrderFile);
    AutoNameSeat := True; 

Open in new window


But for some reason it stops after the for loop
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Perhaps.
for StringlistNum := 0 to StringList.count-1 do
    begin
        CheckName := StringList[StringListNum];
        for NameChar := 0 to (Length(CheckName)-1) do
         begin
          if not (CheckName[NameChar] in Chars) then
           begin
              ShowMessage(CheckName+' has been removed. Names must only contains letters');        //Needs fixing
              StringList.Delete(StringListNum);                                                    //Either deletes everything
            end;
         end;                                                                                   //Or doesn't run code after
    end; 

Open in new window

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Why do would you have the Break in the code anyway?

That would exit the loop.

You are right about reversing the loop, that's what you need when you are deleting.

Does this, without Break, work?
for StringlistNum :=StringList.count-1 downto 0  do
    begin
        CheckName := StringList[StringListNum];
        for NameChar := 0 to (Length(CheckName)-1) do
         begin
          if not (CheckName[NameChar] in Chars) then
           begin
              ShowMessage(CheckName+' has been removed. Names must only contains letters');        //Needs fixing
              StringList.Delete(StringListNum);                                                    //Either deletes everything
            end;
         end;                                                                                   //Or doesn't run code after
    end; 

Open in new window

Author

Commented:
The break was suggested by someone else, it wasn't originally in the code
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
I think the Break might have been why only the first character in each string was being checked.

You could use it to skip to the next word as soon as an invalid character was found in a string but you would need to ensure it was within the begin...end of the inner loop.
for StringlistNum :=StringList.count-1 downto 0  do
    begin
        CheckName := StringList[StringListNum];
        for NameChar := 0 to (Length(CheckName)-1) do
         begin
          if not (CheckName[NameChar] in Chars) then
           begin
              ShowMessage(CheckName+' has been removed. Names must only contains letters');       
              StringList.Delete(StringListNum);              // if an invalid character is found delete the 'current' string                                     
              Break;                                                            // and move onto the next string
            end;
         end;                                                                                  
    end; 

Open in new window

Author

Commented:
norie thanks for the help but I have solved the issue :)
NorieAnalyst Assistant
CERTIFIED EXPERT

Commented:
Apologies, I was just trying to explain why ony the first character was being checked.:)
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
why so much hassle ?

just limit the editor to the keys you allow ...
procedure TfrmTnsEditor.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key not in [#8, #9, #10, #13, #32, 'A'..'Z', 'a'..'z'] then
    Key := #0;
end;

Open in new window


or strip the unwanted chars before adding to the list

and ... a string starts at position 1
 for NameChar := 1 to Length(CheckName) do

Open in new window

Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
empty lines ... you forgot those too

Author

Commented:
Yeh I've since added them in but made sure its not the first char so  IF CheckName[1] = ' ' then delete

Author

Commented:
I managed to solve it although some answers were useful

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.