Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3798

Xojo Documentation: AI hallucinations

$
0
0

I was trying out the new Chinese AI: DeepSeek. I asked it for Xojo code to reverse the order of an array of Points. I had done this earlier using ChatGPT, and I wanted to compare the answers.

In this particular instances, both answers were pretty good. DeepSeek was a little more expansive in its answer. But then it suggested that I could use the Reverse method for arrays if I was using Xojo 2023 Release 1 and later. When I looked at the documentation, I could find no reference for such a method.

I then asked it to provide me a list of the methods in Xojo for working with arrays. It answered.


Certainly! In Xojo, arrays have a variety of methods that allow you to manipulate and work with their contents. Below is a list of commonly used methods for arrays in Xojo, along with a brief description of each.


Array Methods in Xojo

1. Add/Append Elements

  • Append(value)

    • Adds an element to the end of the array.
    • Example: myArray.Append(10)
  • Insert(index, value)

    • Inserts an element at the specified index.
    • Example: myArray.Insert(2, "Hello")

2. Remove Elements

  • Remove(index)

    • Removes the element at the specified index.
    • Example: myArray.Remove(3)
  • RemoveAt(index)

    • Same as Remove. Removes the element at the specified index.
    • Example: myArray.RemoveAt(3)
  • Clear

    • Removes all elements from the array.
    • Example: myArray.Clear

3. Access Elements

  • LastIndex (Property)

    • Returns the index of the last element in the array.
    • Example: Dim lastIdx As Integer = myArray.LastIndex
  • IndexOf(value)

    • Returns the index of the first occurrence of the specified value, or -1 if not found.
    • Example: Dim idx As Integer = myArray.IndexOf("Hello")
  • Value(index)

    • Returns the value at the specified index.
    • Example: Dim val As String = myArray.Value(2)

4. Sorting and Reversing

  • Sort

    • Sorts the array in ascending order.
    • Example: myArray.Sort
  • Sort(comparer As Function)

    • Sorts the array using a custom comparison function.
    • Example: myArray.Sort(AddressOf MyComparerFunction)
  • Reverse

    • Reverses the order of elements in the array (available in Xojo 2023 and later).
    • Example: myArray.Reverse

5. Searching

  • HasValue(value)

    • Returns True if the array contains the specified value.
    • Example: If myArray.HasValue("Hello") Then
  • Find(value)

    • Returns the index of the first occurrence of the specified value, or -1 if not found.
    • Example: Dim idx As Integer = myArray.Find("Hello")

6. Resizing

  • ResizeTo(newSize As Integer)
    • Resizes the array to the specified number of elements.
    • Example: myArray.ResizeTo(10)

7. Copying

  • Clone
    • Returns a new array that is a copy of the original array.
    • Example: Dim newArray() As String = myArray.Clone

8. Miscellaneous

  • Join(separator As String)

    • Joins all elements of the array into a single string, separated by the specified delimiter.
    • Example: Dim result As String = myArray.Join(", ")
  • Pop

    • Removes and returns the last element of the array.
    • Example: Dim lastElement As String = myArray.Pop
  • Shift

    • Removes and returns the first element of the array.
    • Example: Dim firstElement As String = myArray.Shift
  • Push(value)

    • Adds an element to the end of the array (same as Append).
    • Example: myArray.Push("New Item")

Example Usage of Array Methods:

Dim myArray() As String
myArray.Append("Apple")
myArray.Append("Banana")
myArray.Append("Cherry")

// Sort the array
myArray.Sort

// Reverse the array
myArray.Reverse

// Check if the array contains "Banana"
If myArray.HasValue("Banana") Then
    System.DebugLog("Banana is in the array!")
End If

// Join the array into a string
Dim result As String = myArray.Join(", ")
System.DebugLog("Joined array: " + result)

Notes:

  • The availability of some methods (e.g., Reverse) depends on the version of Xojo you are using. For example, Reverse is available in Xojo 2023 Release 1 and later.
  • Arrays in Xojo are zero-based, meaning the first element is at index 0.

This list covers the most commonly used array methods in Xojo. For more details, you can always refer to the Xojo Documentation by pressing F1 in the Xojo IDE or visiting the Xojo Documentation website.


Wow! That was cool. But many of these methods do not exist in Xojo. (It is sort of like a wish list) I am not sure what is happening here. Perhaps it is cross-pollinating with methods from some other language.

I then asked ChatGPT: Is there a Reverse method for arrays in Xojo?


Yes, starting from Xojo 2021 Release 3, there is a built-in Reverse method for arrays, which allows you to reverse the order of elements in an array directly.

Here’s how you can use the Reverse method:

Example

// Create an array of Points
Var points() As Point
points.Add(New Point(1, 1))
points.Add(New Point(2, 2))
points.Add(New Point(3, 3))

// Reverse the array in place
points.Reverse

// Output the reversed array
For Each point As Point In points
    System.DebugLog("Point: (" + point.X.ToString + ", " + point.Y.ToString + ")")
Next

Explanation

• The Reverse method directly reverses the order of elements in the array. It modifies the original array and does not create a new one.

• It’s a simple and efficient way to reverse arrays without writing additional code.

Output for the Example:

Point: (3, 3)
Point: (2, 2)
Point: (1, 1)

Notes:

• The Reverse method is available for all array types (e.g., integers, strings, custom objects like Point).

• If you are using a version of Xojo older than 2021r3, you’ll need to use a manual algorithm, as shown earlier.

Let me know if you need help with anything else! :blush:

Amazing how confidently wrong and specific these answers are. Both insist that there is a Reverse method in Xojo. They disagree as to when it entered the language. DeepSeek says 2023r3 and ChatGPT says 2021r3

11 posts - 9 participants

Read full topic


Viewing all articles
Browse latest Browse all 3798

Trending Articles