Finding Specific item in a list..

BlitzMax Forums/BlitzMax Beginners Area/Finding Specific item in a list..

BachFire(Posted 2007) [#1]
Hello ppl,

As always, when I need to do something in my program, I test it seperately with my own small code. Now I need to pull a specific item from a list, and display it. This easy-to-read code, should explain what I want to do:

SuperStrict
Global Counter:Int
Global CarList:TList=CreateList()

ListAddLast CarList,"Volvo"
ListAddLast CarList,"Blue"
ListAddLast CarList,"$10.000"

ListAddLast CarList,"Chevrolet"
ListAddLast CarList,"Red"
ListAddLast CarList,"$80.000"

ListAddLast CarList,"Ferrari"
ListAddLast CarList,"Red"
ListAddLast CarList,"$200.000"

Print "The Chevrolet costs:"

For Local a:String=EachIn CarList
	If a="Chevrolet" Then Counter = 1
	If Counter = 3 Then Print a ; End
	If Counter < 3 Then Counter:+1
Next


Is there a quicker way, to pull the price of the Chevrolet car? I go through the whole list, until I find it. Isn't there a way to go directly to it? I have been playing around with "ListFindLink", but can't get it to work with a String..?


Dreamora(Posted 2007) [#2]
Yes, use TMap instead of TList
TMap is an assoziative structure, which lets you retrieve the value to a given key (in this case the name) in O(log(N)) time (N is the number of elements stored in the TMap), which is significantly faster than the O(n) of the list and besides that allows you to bind it uniquely to a name.

The alternative to this is an own class CAR which has a field of type TLink, used for sorting if this is the reason you put it into the list.
the value would then be a field as well.


Brucey(Posted 2007) [#3]
Use a Car Type instead ?

SuperStrict
Type Car
  field model:String
  field color:String
  field price:Float

  Function Create:car(model:String, color:String, price:Float)
    Local this:Car = new Car

    this.model = model
    this.color = color
    this.price = price

    Return this
  End Function
End type

Global CarList:TList=CreateList()

ListAddLast CarList, Car.Create("Volvo", "Blue", 10000)
ListAddLast CarList, Car.Create("Chevrolet", "Red", 80000)
ListAddLast CarList, Car.Create("Ferrari", "Red", 200000)

Print "The Chevrolet costs:"

For Local a:Car = EachIn CarList
    If a.model = "Chevrolet" Then 
        Print a.price
        Exit
    End If
Next


Just a thought ;-)


Brucey(Posted 2007) [#4]
Or put it in a Map like wot Dream said.


BachFire(Posted 2007) [#5]
Just a thought ;-)


Good one.. You know, I would probably take about a week to come up with this stuff on my own? If that fast, even.. I love you guys, for providing this kind of unconditional help. Gives me a knowledge boost. Especially with the code examples, which helps me understand how something works.. ;)

Thank you, both of you!


tonyg(Posted 2007) [#6]
You might want to review Wave's excellent tutorial . It covers many of the queries you have.


Grey Alien(Posted 2007) [#7]
Plus if you knew the item's index in the list you could use List.ValueAtIndex:Object( index )

But you'll have to typecast the object returned back into your type e.g.

selected:TCar = TCar(CarList.ValueAtIndex(5))


Dreamora(Posted 2007) [#8]
sorry GA but thats a beginners advice a more experienced BM programmer shouldn't give.

ValueAtIndex loops through the list as well, a list never has an indexed access.

you would need to make an array from it to have direct access to an index or save the TLink object and access this one


tonyg(Posted 2007) [#9]
Bachfire, my view is, if you have a limited number of objects (e.g. 100 or so) use the loop method a la Brucey. For a beginner this is the least complicated way and will be quick enough. Once you start getting to a 1000s of object you might need to look into TMaps or saving the TLink in the type. These are considered a bit more complex (although some will argue otherwise).
I don't know what you're doing but I didn't know Tmaps existed for the first year and didn't really miss them.
For much of my 'tinkering' I still use the looping method. It helps to be aware of optimisations but you don't necessarily need to use them.
Run through that tutorial I link to as it REALLY helped me and many others here.


BachFire(Posted 2007) [#10]
Yes, I'm easily confused. I'm pretty sure it's because I have a vision of something I want to make, and then want to realize it too fast. Then I get buried in complex stuff, that probably wouldn't have been so complex if I started from the bottom. Some of my code, I don't even know why it works.. I think what I'm doing is a little unrealistic for my current knowledge, after all..

I have decided to stop my project for now, and do some of the nice tutorials, and create some small things to hone a bit. Then return to my project in a week or whenever I feel a bit more ready.

I want to be good at this.

Thanks all! ;)


tonyg(Posted 2007) [#11]
Good approach. I was a little disappointed with BlitzMax when I started. I now put this down to 'assuming' I knew the best way to use it. I was wrong. When I started again, worked through those tutorials and built up slowly I began to realise how fantastically powerful and expandable the language is. Now, I really find it a pleasure to use. There are a few omissions which still annoy me but that's offset by the 'plus' points.
If you get stuck there are always these forums.


Grey Alien(Posted 2007) [#12]
ValueAtIndex loops through the list as well
sure but it means HE doesn't have to write the loops :-)