Array locator method 'find_index' not working on a string queue

Hello, I’ve been having a problem with a string queue that I wish to search. Made worse by the LRM (1800-2017). Let me share what I have done.


module find_markers;
string arr[$];
image_tests img1(.arr(arr)); //String queue imported from another module. Data is similar to this: amb124hk2k3b2jb42223fmf...

int buffer [$]; //To store indices of search string. So if I want to search for 'amb' it should give the indices 0, 1 and 2 in return from above data. Or if I give '2' it would return 4,8,12,16,17,18...

always_comb 
begin
string x;
buffer = arr.find_index() with (x=="b"); //LRM says on page 166 that we have to use find_index, but it returns an error. So I used find_index() instead. Even for one character such as 'b' it returns different values each time for buffer.
$display("Buffer = %d",buffer); //This returns a different value for the same query. A very large number. If I run foreach on it, my simulator does not show any output (I am using Verilator).
end

endmodule

What am I doing wrong here? Can anyone guide me?

In reply to Dev_Engine:

There are some inconsistencies in your question, so some clarification is needed.

The string data type is an ordered collection of characters. You state that your data is “amb124hk2k3b2jb42223fmf…”, which would be a single string. An queue of strings would be multiple strings, not individual characters.

You don’t show the output of your module ‘image_tests’, so it is difficult to determine if your connections are correct.

The find_index() function will not search each string. Searching for “b”, or any other single character, won’t work. The function will only match the entire string.

Please check your data types and provide clarifying information.

In reply to cgales:

In reply to Dev_Engine:
There are some inconsistencies in your question, so some clarification is needed.
The string data type is an ordered collection of characters. You state that your data is “amb124hk2k3b2jb42223fmf…”, which would be a single string. An queue of strings would be multiple strings, not individual characters.
You don’t show the output of your module ‘image_tests’, so it is difficult to determine if your connections are correct.
The find_index() function will not search each string. Searching for “b”, or any other single character, won’t work. The function will only match the entire string.
Please check your data types and provide clarifying information.

Thanks for replying. This is the first module ‘image_tests.sv’:


module image_tests(arr);
output string arr[$];

string char;
int file;
always_comb
begin
    file = $fopen("./donald.txt","r"); // Input file that is basically a hex dump of a jpeg image
    
    while(!$feof(file))
    begin
        char = $fgetc(file); //My simulator doesn't support $readmemh so I had to use this
        arr.push_back(char);
        
    end
    foreach (arr[i])
        begin
        if(arr[i] == "\n") //remove newline characters
        begin
            arr.delete(i);
        end
        $display("Array value = %s at index %d",arr[i],i); //Display each character of the queue with a separate index for each.
        end
    
    
    
    $fclose(file);
    $fclose(f_out);
end  
endmodule

This produces a very large string queue ‘arr’ that starts this way:
ffd8ffe000104a46494600010101012c012c0000ffe100d5457869660000
49492a000800000001000e010200b30000001a0000000000000031352046
656220313939363a2020416c6c616e20446f6e616c64206f6620536f7574
68204166726963612…

This queue is imported into the module in OP where I just want to read every character. I tried getc but it doesn’t work. I tried using arr[i] on RHS to assign it to a variable, that didn’t work either.

In reply to Dev_Engine:

Your use of strings is incorrect.

The function $fgetc() doesn’t return a string, it returns an integer type. You should get an incompatible assignment error when trying to assign the return of $fgetc() to a string type.

You likely want to use ‘int’ instead of ‘string’ throughout your code.

In reply to cgales:

In reply to Dev_Engine:
Your use of strings is incorrect.
The function $fgetc() doesn’t return a string, it returns an integer type. You should get an incompatible assignment error when trying to assign the return of $fgetc() to a string type.
You likely want to use ‘int’ instead of ‘string’ throughout your code.

I did replace ‘int’ wherever ‘string’ was but it gives an error that ‘cannot convert ‘unsigned int’ to ‘const std::__cxx11::basic_string&’

However with ‘string’ I read the file just fine but am stuck in the problem in OP.

P.S Is there any way to convert this string queue into a single string? I tried this in an always_latch:


foreach(arr[index])
begin
i=2*index;
j=i+1;
if(j<=arr.size)
str_arr={str_arr,arr[i],arr[j]}; //Concatenation
else
break;
end


But this broke down too with a warning: ‘Signal unoptimizable: Feedback to clock or circular logic’

In reply to Dev_Engine:

That error indicates that your simulator doesn’t function per the LRM. This is one of the pitfalls of using an open-source simulator.

You can refer to this implementation on EDA Playground which works on all commercial simulators.