Dave's Brain

Browse - programming tips - perl array append prepend

Date: 2011aug29
Language: perl

Q.  How do I prepend/append in perl?
(Add at the start of an array, add at the end of an array.)

A. Prepend with unshift() - eg:

	my(@a);

	unshift(@a, 'three');
	unshift(@a, 'two');
	unshift(@a, 'one');

	is the same as @a = qw(one two three)

A.  Append with push() - eg:

	my(@a);

	push(@a, 'one');
	push(@a, 'two');
	push(@a, 'three');

	is the same as @a = qw(one two three)
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: