Lasso Soft Inc. > Home

[array_flip_9]

Linkarray_flip_9
AuthorJonathan Guthrie
CategoryArray
Version9.x
LicensePublic Domain
Posted05 Nov 2010
Updated06 Nov 2010
More by this author...

Description

Lazy way to reverse the order of key/value pairs in a pair array.

NOTE: This is a Lasso 9 port of Jason Huck's array_flip

http://tagswap.net/array_flip

Also included is a method that extends the Lasso 9 array type, adding ->flip as a member tag. Thanks to Jolle Carlestam for the inspiration :)

Sample Usage

var(unflipped = array( 
    'a' = 1, 
    'b' = 2, 
    'c' = 3 
	)
)
 
var(flipped = array_flip($unflipped)) 

'unflipped: '		+ $unflipped
'flipped: '		+ $flipped
'l9 method flipped: '	+ $flipped->flip &

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define array_flip(in) => {
	// Returns the given pair array with the keys and values inverted.
    local(out = array) 
	with i in #in 
	where #i->isA('pair')
	do => { 
		#out->insert(#i->second = #i->first)
	}
	return #out
}

// OR

define array->flip() => {
	loop(.size) => {
		if(.get(loop_count)->isa('pair')) => {
			.get(loop_count) = pair(.get(loop_count)->second = .get(loop_count)->first)
		}
	}
}

Related Tags

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft