Lasso Soft Inc. > Home

[array_shuffle]

Linkarray_shuffle
AuthorJason Huck
CategoryArray
Version8.x
License
Posted24 Oct 2005
Updated24 Oct 2005
More by this author...

Description

Returns the given array in a new, random order.

Sample Usage

var('orderedItems') = array(1,2,3,4,5);
var('shuffledItems') = array_shuffle($orderedItems);
$orderedItems;

// -> array: (3),(2),(1),(5),(4)

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_tag(
	'shuffle',
	-namespace='array_',
	-priority='replace',
	-description='Returns the given array in a new, random order.'
);
	fail_if(
		!params->size || !params->first->isa('array'),
		-1,
		'[array_shuffle] requires an array as a parameter.'
	);
	
	local(
		'in' = params->first,
		'out' = array
	);
	
	#out->reserve(#in->size);
	
	while(#in->size);
		local('x') = math_random(
			-lower=1, 
			-upper=#in->size
		);

		#out->insert(#in->get(#x));	
		#in->remove(#x);
	/while;
	
	return(#out);
/define_tag;

Related Tags

Comments

29 Mar 2010, Dominique Guardiola Falco

I love tagswap

Just typed 'lasso array shuffle' in Google, tadaaa, thanks jason!

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