[array_flip_9]

Description

Link: [array_flip_9]
Author: Jonathan Guthrie
Category: Array
Version: 9.x
License: Public Domain
Posted: Nov. 05, 2010
Updated: May. 17, 2012
More by this author...

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 :)

Parameters

none


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net