2

I am using LUA as part of a minecraft mod - I am not sure how much that affects things - see openComputers.

The code attempts to match a given string to one obtained by iterating over a series of slots containing items which have names.

The chunks of code that are important are:

    term.write("enter name")
    name=term.read()

returns a string e.g. "Forest" without the quotation marks. I have also checked the datatype.

Then later the code parses an inventory of items looking for one with "Forest" in its name. The first line for what it's worth is somewhat specific to the game but the output should render that unimportant.

    item = storage.getStackInSlot(sides.top,i)

this returns a bunch of stuff but is then refined with:

    item_name = item.label

which returns "Bee Sample - Species: Forest" which I have also checked is definitely a string.

The code then returns nil for the line where the match/find should return something != nil. I even print the two strings before the string.find operation.

    match = string.match(item_name, name, 1, true)
    print(match)

returns nil.

I have no clue why this would return false. I have tested the match and find functionality separately in interpreter with stuff like:

    a=bic
    b=ambico
    string.find(b,a)

and it was fine.

Sorry if this is super obvious and many thanks

ratchet600
  • 69
  • 4
  • May be `name` is `"Forest\n"` instead of `"Forest"` ? Check the output of `print(name:byte(1,-1))` – Egor Skriptunoff Nov 25 '18 at 12:41
  • Hi, So that returns ascii 10 at the end which is indeed line return but shouldn't it find the matching substring regardless similar to if \n was say "D"? I modified the code to be string.find(item_name, name.."\n") and it still doesn't find it :/ . – ratchet600 Nov 25 '18 at 15:54
  • `name.."\n"` is `"Forest\n\n"` now :-) – Egor Skriptunoff Nov 25 '18 at 18:58
  • You need to remove the final `"\n"`. The pattern-matching functions do not ignore any characters in the pattern. `string.find("Forest", "Forest\n")` fails. – cyclaminist Nov 25 '18 at 22:09
  • Hi all, Thanks for your input. So after putting a name = string.gsub(name, "n", "") clause in before hand to remove the line feed the name:byte(1,-1) still shows a line feed at the end. This happens with "/n", "%n", and "%/n" so it isn't recognizing the character.Moreover if i use string.gmatch it returns true but it also does it for the occurrences where item_name does not actually contain name "Forest" in the examples so far. I feel like we are close... – ratchet600 Nov 26 '18 at 22:56

2 Answers2

1

Try stripping all trailing whitespace off the read name, as follows:

name = term.read():gsub('%s+$', '')

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
1

I have found a solution, Using string.gsub with the ascii code instead of the character itself solves the problem. For those who encounter the same problem:

newstring = string.gsub(string, "[\10]", "")

This removes the line feed and allows string matching henceforth.

ratchet600
  • 69
  • 4