Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Examples for list.n7
#1
Hi Marcus,

I'm trying to use the "list.n7" library, herewith the code :
Code:
include "list.n7"

' 1. Create a List object
myList = List()

' 2. Add items using the Add function
for i = 0 to 5
    myList.Add(i)
next

wln
' 3. Display initial list
wln "=== Initial List ==="
wln "Size:"+ myList.Size()
wln "Contents:"+ myList.ToString()

wln
' 4. Test Contains and Add
wln "=== Contains Test ==="
wln "Contains 6?"+ myList.Contains(6)
myList.Add(6)
wln "After Add(6), Contains 6?"+ myList.Contains(6)
wln "Contents:"+ myList.ToString()

wln
' 5. Sort Ascending
wln "=== Sort Ascending ==="
' Add some unsorted numbers first
myList.Add(2)
myList.Add(8)
myList.Add(1)
wln "Before sort:"+ myList.ToString()
myList.Sort(myList.ASCENDING)
wln "After Sort(ASCENDING):"+ myList.ToString()

wln
' 6. Sort Descending
wln "=== Sort Descending ==="
myList.Sort(myList.DESCENDING)
wln "After Sort(DESCENDING):"+ myList.ToString()

wln
' 7. Sorted (Returns copy, doesn't modify original)
wln "=== Sorted (Returns Copy) ==="
myList.Add(5)
myList.Add(3)
wln "Original before Sorted():"+ myList.ToString()
sortedCopy = myList.Sorted(myList.ASCENDING)
wln "Sorted copy (ascending):"+ sortedCopy.ToString()
wln "Original unchanged:"+ myList.ToString()

wln
' 8. SortByField (For lists containing tables/objects)
wln "=== SortByField Example ==="
people = List()

person1 = []
person1["name"] = "Charlie"
person1["age"] = 35
people.Add(person1)

person2 = []
person2["name"] = "Alice"
person2["age"] = 25
people.Add(person2)

person3 = []
person3["name"] = "Bob"
person3["age"] = 30
people.Add(person3)

wln "People before sort:"+ people.ToString()
people.SortByField("age", people.ASCENDING)
wln "People sorted by age (ascending):"+ people.ToString()

people.SortByField("name", people.DESCENDING)
wln "People sorted by name (descending):"+ people.ToString()

system("pause")

Unfortunately some outputs are unreadable 8. SortByField (For lists containing tables/objects). 
Could you please advise me ? It's not urgent, you may reply when you have time later. 
Thank you

[Image: bmoRG.jpg]
Reply
#2
(03-30-2026, 07:47 AM)1micha.elok Wrote: Hi Marcus,

I'm trying to use the "list.n7" library, herewith the code :
Code:
include "list.n7"

' 1. Create a List object
myList = List()

' 2. Add items using the Add function
for i = 0 to 5
    myList.Add(i)
next

wln
' 3. Display initial list
wln "=== Initial List ==="
wln "Size:"+ myList.Size()
wln "Contents:"+ myList.ToString()

wln
' 4. Test Contains and Add
wln "=== Contains Test ==="
wln "Contains 6?"+ myList.Contains(6)
myList.Add(6)
wln "After Add(6), Contains 6?"+ myList.Contains(6)
wln "Contents:"+ myList.ToString()

wln
' 5. Sort Ascending
wln "=== Sort Ascending ==="
' Add some unsorted numbers first
myList.Add(2)
myList.Add(8)
myList.Add(1)
wln "Before sort:"+ myList.ToString()
myList.Sort(myList.ASCENDING)
wln "After Sort(ASCENDING):"+ myList.ToString()

wln
' 6. Sort Descending
wln "=== Sort Descending ==="
myList.Sort(myList.DESCENDING)
wln "After Sort(DESCENDING):"+ myList.ToString()

wln
' 7. Sorted (Returns copy, doesn't modify original)
wln "=== Sorted (Returns Copy) ==="
myList.Add(5)
myList.Add(3)
wln "Original before Sorted():"+ myList.ToString()
sortedCopy = myList.Sorted(myList.ASCENDING)
wln "Sorted copy (ascending):"+ sortedCopy.ToString()
wln "Original unchanged:"+ myList.ToString()

wln
' 8. SortByField (For lists containing tables/objects)
wln "=== SortByField Example ==="
people = List()

person1 = []
person1["name"] = "Charlie"
person1["age"] = 35
people.Add(person1)

person2 = []
person2["name"] = "Alice"
person2["age"] = 25
people.Add(person2)

person3 = []
person3["name"] = "Bob"
person3["age"] = 30
people.Add(person3)

wln "People before sort:"+ people.ToString()
people.SortByField("age", people.ASCENDING)
wln "People sorted by age (ascending):"+ people.ToString()

people.SortByField("name", people.DESCENDING)
wln "People sorted by name (descending):"+ people.ToString()

system("pause")

Unfortunately some outputs are unreadable 8. SortByField (For lists containing tables/objects). 
Could you please advise me ? It's not urgent, you may reply when you have time later. 
Thank you

[Image: bmoRG.jpg]


If you want a list's ToString function to work on lists of objects/tables, the objects themselves need a ToString function. Here's a modified version of your code:

Code:
include "list.n7"

' 1. Create a List object
myList = List()

' 2. Add items using the Add function
for i = 0 to 5
    myList.Add(i)
next

wln
' 3. Display initial list
wln "=== Initial List ==="
wln "Size:"+ myList.Size()
wln "Contents:"+ myList.ToString()

wln
' 4. Test Contains and Add
wln "=== Contains Test ==="
wln "Contains 6?"+ myList.Contains(6)
myList.Add(6)
wln "After Add(6), Contains 6?"+ myList.Contains(6)
wln "Contents:"+ myList.ToString()

wln
' 5. Sort Ascending
wln "=== Sort Ascending ==="
' Add some unsorted numbers first
myList.Add(2)
myList.Add(8)
myList.Add(1)
wln "Before sort:"+ myList.ToString()
myList.Sort(myList.ASCENDING)
wln "After Sort(ASCENDING):"+ myList.ToString()

wln
' 6. Sort Descending
wln "=== Sort Descending ==="
myList.Sort(myList.DESCENDING)
wln "After Sort(DESCENDING):"+ myList.ToString()

wln
' 7. Sorted (Returns copy, doesn't modify original)
wln "=== Sorted (Returns Copy) ==="
myList.Add(5)
myList.Add(3)
wln "Original before Sorted():"+ myList.ToString()
sortedCopy = myList.Sorted(myList.ASCENDING)
wln "Sorted copy (ascending):"+ sortedCopy.ToString()
wln "Original unchanged:"+ myList.ToString()

wln
' 8. SortByField (For lists containing tables/objects)
wln "=== SortByField Example ==="
people = List()

' marcus: create new person objects using a function instead, less typing.
function Person(name, age)
    person = []
    person.name = name ' same thing as person["name"] but faster.
    person.age = age
    ' a function that a list can use when printing.
    person.ToString = function()
        return this.name + " (" + this.age + ")"
    endfunc
    return person
endfunc

'person1 = []
'person1["name"] = "Charlie"
'person1["age"] = 35
'people.Add(person1)

'person2 = []
'person2["name"] = "Alice"
'person2["age"] = 25
'people.Add(person2)

'person3 = []
'person3["name"] = "Bob"
'person3["age"] = 30
'people.Add(person3)

' marcus: use Person constructor function.
'person1 = Person("Charlie", 35)
'people.Add(person1)

'person2 = Person("Alice", 25)
'people.Add(person2)

'person3 = Person("Bob", 30)
'people.Add(person3)

' marcus: or, with less typing:
people.Add(Person("Charlie", 35))
people.Add(Person("Alice", 25))
people.Add(Person("Bob", 30))

wln "People before sort:"+ people.ToString()
people.SortByField("age", people.ASCENDING)
wln "People sorted by age (ascending):"+ people.ToString()

people.SortByField("name", people.DESCENDING)
wln "People sorted by name (descending):"+ people.ToString()

system("pause")
Reply
#3
Thank you, Marcus....I might Big Grin use the list.n7 library to develop a classic detective game inspired by Where in the World is Carmen Sandiego ? (1985)  by Broderbund. 
The first step is to initialize the game world by setting up international locations and the suspect  database

Code:
' ==========================================
' INITIALIZE GAME WORLD
' ==========================================

' 1. Setup the 3 International Locations
visible world = List()
world.Add(Location("London", "The thief was heard speaking French.", "The thief wears a bright RED hat."))
world.Add(Location("Paris", "The thief bought a ticket to Egypt.", "The thief loves eating SPICY food."))
world.Add(Location("Cairo", "The thief is hiding nearby!", "The thief has SHORT hair."))

' 2. Setup the Suspect Database (Interpol Files)
visible suspects = List()
suspects.Add(Thief("Carmen Sandiego", "Female", "Short", "Spicy"))
suspects.Add(Thief("Count Clever", "Male", "Long", "Sweet"))
suspects.Add(Thief("Baron Gruff", "Male", "Bald", "Salty"))
suspects.Add(Thief("Lady Luck", "Female", "Short", "Sour"))
suspects.Add(Thief("Doc Doom", "Male", "Long", "Spicy"))
Reply
#4
(03-31-2026, 04:19 AM)1micha.elok Wrote: Thank you, Marcus....I might Big Grin use the list.n7 library to develop a classic detective game inspired by Where in the World is Carmen Sandiego ? (1985)  by Broderbund. 
The first step is to initialize the game world by setting up international locations and the suspect  database

Code:
' ==========================================
' INITIALIZE GAME WORLD
' ==========================================

' 1. Setup the 3 International Locations
visible world = List()
world.Add(Location("London", "The thief was heard speaking French.", "The thief wears a bright RED hat."))
world.Add(Location("Paris", "The thief bought a ticket to Egypt.", "The thief loves eating SPICY food."))
world.Add(Location("Cairo", "The thief is hiding nearby!", "The thief has SHORT hair."))

' 2. Setup the Suspect Database (Interpol Files)
visible suspects = List()
suspects.Add(Thief("Carmen Sandiego", "Female", "Short", "Spicy"))
suspects.Add(Thief("Count Clever", "Male", "Long", "Sweet"))
suspects.Add(Thief("Baron Gruff", "Male", "Bald", "Salty"))
suspects.Add(Thief("Lady Luck", "Female", "Short", "Sour"))
suspects.Add(Thief("Doc Doom", "Male", "Long", "Spicy"))

Nice! Soon your code will be fully object oriented Smile
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)