ChangeCurrentElement

Found an issue in the documentation ? Please report it here !

Moderator: Documentation Editors

User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

ChangeCurrentElement

Post by Josh »

The example in the help for ChangeCurrentElement is not a running code. It's only a part.

I'm not sure this is a bug or it is by design, but other examples in help are full running snippets.
sorry for my bad english
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: ChangeCurrentElement

Post by Thunder93 »

I've seen some that weren't full.

I guess it's expected that someone has already little knowledge working with lists.


Here's full version demonstrating the usage.

Code: Select all

Structure BasicStructure
  Name.s
EndStructure

NewList mylist.BasicStructure()

AddElement(mylist())
mylist()\Name = "One"

AddElement(mylist())
mylist()\Name = "Two"

AddElement(mylist())
mylist()\Name = "Three"

AddElement(mylist())
mylist()\Name = "Four"

SelectElement(mylist(), 1) ; Changing from last element to second element

*Old_Element = @mylist()   ; Get the address of the current element

Debug "At position "+ListIndex(mylist())+", the value is "+Str(mylist())

ResetList(mylist())        ; Perform a search for all elements named
While NextElement(mylist()); "Three" and change them to "3"
  If mylist()\name = "Three"
    mylist()\name = "3"
  EndIf
Wend

ChangeCurrentElement(mylist(), *Old_Element) ; Change from last element to previously saved element position.

Debug "At position "+ListIndex(mylist())+", the value is "+Str(mylist())
Edited: Included change mentioned by MrMat.
Last edited by Thunder93 on Thu Mar 16, 2017 6:30 pm, edited 1 time in total.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Re: ChangeCurrentElement

Post by MrMat »

Hi Thunder93,

Thanks for your example. This is incredibly minor, but in case it gets pasted verbatim into the docs:

Code: Select all

While NextElement(mylist()); "John" and change them to "J" 
becomes:

Code: Select all

While NextElement(mylist()); "Three" and change them to "3" 
Cheers,
Mat
Mat
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: ChangeCurrentElement

Post by Thunder93 »

Made the change in my previous post. Might be too long winded though, .. that example.

Maybe something like;

Code: Select all

Structure BasicStructure
  Name.s
EndStructure

NewList mylist.BasicStructure()

AddElement(mylist()) : mylist()\Name = "HOME"

AddElement(mylist()) : mylist()\Name = "ONE"

*Old_Element = @mylist()   ; Get the address of the current element

AddElement(mylist()) : mylist()\Name = "TWO"

AddElement(mylist()) : mylist()\Name = "THREE"

Debug "At position "+ListIndex(mylist())+", the value is "+mylist()\Name

ChangeCurrentElement(mylist(), *Old_Element) ; Change from last element to previously saved element position.

Debug "Now at position "+ListIndex(mylist())+", the value is "+mylist()\Name
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ChangeCurrentElement

Post by mestnyi »

Not there wrote :oops:
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: ChangeCurrentElement

Post by mestnyi »

Syntax

ChangeCurrentElement(List(), *NewElement)
Description
Changes the current element of the specified list to the given new element. This function is very useful if you want to "remember" an element, and restore it after performing other processing.
Parameters

List() The name of the list, created with the NewList function. You must include the brackets after the list name.
*NewElement The new element to set as the current element for the list. The element must be a pointer to another element which exists in this list. You should get this address by using the @ operator on the list name and not through any other method.
Return value

None.
In the help it is written that does not return anything then as it that that returns.

Code: Select all

Macro IsChangeCurrentElement(List, Element) 
  Bool(Element And ChangeCurrentElement(List, Element)) 
EndMacro

Structure BasicStructure
  Name.s
EndStructure

NewList mylist.BasicStructure()

AddElement(mylist()) : mylist()\Name = "HOME"

AddElement(mylist()) : mylist()\Name = "ONE"

*Old_Element = @mylist()   ; Get the address of the current element

AddElement(mylist()) : mylist()\Name = "TWO"

AddElement(mylist()) : mylist()\Name = "THREE"

Debug "At position "+ListIndex(mylist())+", the value is "+mylist()\Name


; *Old_Element = ChangeCurrentElement(mylist(), *Old_Element) ; Uncomment to see
; *Old_Element = 0 ; Uncomment to see

If IsChangeCurrentElement(mylist(), *Old_Element) ; Change from last element to previously saved element position.
  Debug "Now at position "+ListIndex(mylist())+", the value is "+mylist()\Name
EndIf
Can someone explain this to me?
I really need to know if the pointer is valid if yes then do this and then that ...
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: ChangeCurrentElement

Post by #NULL »

Can someone explain this to me?
If the return value is 'none' then there is no point in using it.
I really need to know if the pointer is valid if yes then do this and then that ...
You should always know if your pointer is valid. null it as soon as the pointee becomes invalid. You could loop throught the list and compare each element address with your pointer, but there is no guarantee a matching address means it is still the object you assigned to the pointer:

Code: Select all

NewList l.i()
AddElement(l()) : l() = 111
AddElement(l()) : l() = 222
AddElement(l()) : l() = 333

LastElement(l())
*p.Integer = @l()
Debug *p\i

DeleteElement(l())

AddElement(l()) : l() = 444
Debug *p\i

Code: Select all

333
444
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: ChangeCurrentElement

Post by Andre »

I would be happy to extend the PB docs with one or more of the posted examples. :D
But before of that, Fred should take a look according return value and related notes / examples...
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: ChangeCurrentElement

Post by #NULL »

IMO the example code should be concise and down to the actual command, not fiddling with structures or ListIndex()

Code: Select all

NewList myList()

AddElement(myList())
myList() = 100

AddElement(myList())
myList() = 200
*element = @myList()

AddElement(myList())
myList() = 300

Debug myList()                             ; 300 (last element)
ChangeCurrentElement(myList(), *element)   ; restore list position
Debug myList()                             ; 200

ForEach myList()
  If @myList() = *element
    Debug "element: " + myList()           ; element: 200
  EndIf
Next
The current example code shows more or less what today would be done via PushListPosition() and PopListPosition(). These might be mentioned in 'see also'.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: ChangeCurrentElement

Post by Andre »

Have added the last example of #NULL for now, also the Push/Pop references.

Some clarification about the return value is up to Fred. Else this topic can be closed.
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: ChangeCurrentElement

Post by Andre »

@Fred / all: Is still more/better decription needed about the ReturnValue?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply