0

I put in this code into Delphi but it came up with error

Operator not applicable to this operand type

I have no idea what that means and why it's coming up. I'm trying to only get the surname from an email with the format

[email protected]

where i is the first initial and yy is the year of joining the company. I know I can't just use midstr so I'm trying to use NOT commands to eliminate the rest of the email to leave the surname.

Here's the code:

uses
  System.SysUtils,
  strutils;

var email: string;

begin
writeln('input your email');
readln(email);
writeln('Your last name is ', (NOT leftstr(email,2)) AND (NOT rightstr(email,10)));
readln;
end.
7
  • 2
    The error seems clear. not does not apply to strings. What do you want to do? Commented Sep 18, 2015 at 16:25
  • I want to output everything before the @ symbol but I'm not too sure how I could do that without using not. I guess I presumed that if you have OR and AND you could use NOT as well Commented Sep 18, 2015 at 16:28
  • 2
    The error is extremely clear. You can't apply boolean operation on strings. What part is confusing you? Commented Sep 18, 2015 at 16:29
  • Forget the NOT. It doesn't work the way you think it does. You should read the documentation for the language elements you are trying to use. You need to isolate the substring before the @ character first. Right now your code makes no reference to the '@' character or its ASCII representation, so that's an inhibitor to doing step 1. Once you isolate the substring before the @ then you can get the first and last two characters of that substring using string functions. Commented Sep 18, 2015 at 16:40
  • 1
    I find it an iteresting idea though: having a bitfield determine which positions into a string to keep, and use logical operators on the bitfield first (and, not, or, xor). I've been using something similar with parsers, but turned out more like an array of splices (parameters to the Copy function) Commented Sep 19, 2015 at 8:42

3 Answers 3

2

The and, or and not operators do not apply to strings. Your code is far from what you need.

But the biggest criticism I have here is the mixing of the string processing with your output code. I'd write a pure string processing helper.

function DecodeEmailAddressOK(
  const Address: string;
  out Initial: string;
  out Surname: string;
  out Year: Integer
): Boolean;
var
  AtPos: Integer;
begin
  AtPos := Pos('@', Address);
  if AtPos < 5 then
    Exit(False);
  Initial := Copy(Address, 1, 1);
  Surname := Copy(Address, 2, AtPos-3);
  Result := TryStrToInt(Copy(Address, AtPos-2, 2), Year);
end;

The error checking is very crude. I'm sure you'll be able to do better.

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

Comments

1

Sorry, but I confess I can't make head or tails of your code. You are definitely on the wrong track.

But, I can show you some pretty simple code that should get you going:

Please be warned, I'm writing this code off the top of my head. But don't worry, someone will be very quick to correct my mistakes. :-)

function ExtractNameFromEmail(aEmail: string): string;
var
  PositionOfAtSymbol: Integer;
begin
  if not aEmail.Contains('@') then
  begin
    WriteLn('I don''t think you passed an email address');
    Exit;
  end;

  PositionOfAtSymbol := Pos('@', aEmail);
  Result := Copy(aEmail, 1, PositionOfAtSymbol - 1);
end;

That will give you what you appear to be looking for. It takes a bit of counting (notice the need for the "- 1" in the final line) but that does what you want.

2 Comments

Pedant mode engaged. Mixing helpers and old style non helper functions feels odd. Also it's needless to walk the string twice. Call Pos once and replace Contains with a test that the Pos returned a non-zero value.
If we are very pedantic then there should be a `raise EInvalidOpException.CreateFmt( '"%s" is not a valid email address', [aEmail] );'.
1

I am afraid you have completely misunderstood the meaning of the logical operators and, or, and not. These are not used with strings, but with booleans (logical values, which are either true or false).

not is a unary operator which returns true if the operand is false, and returns false if the operand is true. Thus, not true yields false, and not false yields true.

For example, if you have a boolean variable PasswordTooSimple (the typed password is too simple), you might want to set CanContinue := not PasswordTooSimple somewhere in your logic.

and is a binary operator on booleans, which returns true if and only if both of its operands are true. For example, CanContinue := ValidUser and not PasswordTooSimple.

or is a binary operator on booleans, which returns true if and only if at least one of its operands is true. For example, CanContinue := (PaymentComplete or NoncommersialVersion) and not PasswordTooSimple.

If PaymentComplete, NoncommersialVersion and PasswordTooSimple are false, true, and false, respectively, then, applying these rules, we find that PaymentComplete or NoncommersialVersion will evaluate to true, that not PasswordTooSimple will evaluate to true, and -- therefore -- the entire expression (PaymentComplete or NoncommersialVersion) and not PasswordTooSimple = true and true = true, so that CanContinue will be true. For example, you might have btnContinue.Enabled := CanContinue.

By the way, now you know what the compiler means by "Operator not applicable to this operand type". Indeed, the error message is spot on: the not operator is not applicable to string operands.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.