Inparablog

A comparative genomics and bioinformatics blog

  • Home
  • About
  • Perl
    • Arrays
    • RegExp
  • Links
  • Photos
  • Contact
RSS

Arrays

This page will show different ways to handling arrays and some tricks as well. It is not complete, by far.

An array is a data type in Perl which is basically a list of values you want to store.

my @array = ('apple','flour','cinnamon','suggar','butter');

A very simple way or making an array of 10 elements

my @array = (1..10);

This is similar to:

my @array = (1,2,3,4,5,6,7,8,9,10);

but much shorter.

Another way of making an array of 10 elements, but now all have the same value.

my @array =  (0) x 10;

Now your array will look like (0,0,0,0,0,0,0,0,0,0);

I see this quite often when someone wants to change or do something with each element in an array. I think it is not the most elegant solution, also it is more error prone.

for (my $i = 0; $i < = scalar @array -1; $i++) {
	$array[$i] = 1;
}

Now your array will look like (1,1,1,1,1,1,1,1,1,1);

Many people forget that $#array will give you the same number as “scalar @array -1″. I find this much better readable.

foreach my $i (0..$#array) {
	$array[$i] = 2;
}

Now your array will look like (2,2,2,2,2,2,2,2,2,2);

Or when you only want to manipulate each element in an array, just modify the $_ in a foreach loop!

foreach (@array) {
	$_ = 3;
}

Now your array will look like (3,3,3,3,3,3,3,3,3,3);

You could also consider ‘map’

map { $_ = 4 } @array;

Now your array will look like (4,4,4,4,4,4,4,4,4,4);

Infinitely cycle through each value of an array (make sure you can escape the loop):

while (my $element = shift @array) {
	push @array, $element;
	print "$element\n";
}

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*


question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Search

  • The author

    Gravatar My name is John van Dam and I am a Post-Doc at St. Radboud University Medical Center (NL). My research involves bioinformatics and comparative genomics on cilia and signal transduction pathways.
  • About me

    • LinkedIn Profile
    • Mendeley Profile
    • Research Blogging Profile
  • Bioinformatics Blogs

    • Bioinformatics
    • Bioinformatics Zen
    • Fisheye Perspective
    • nodalpoint
    • Omics! Omics!
    • Public Rambling
    • The Tree of Life
    • What You’re Doing Is Rather Desperate
    • YOKOFAKUN
  • Perl

    • Beginning Perl
    • Bio::Perl
    • PerlMonks
  • Tags

    Backreferences BBC Conference Cordyceps E-values Fungus Hardware Homology Insects Lightning Mac OS Meiosis Office paradox permalinks PhD Phylogenetic tree phylogeny Python Quadrupel radio Regexp Regular Expressions research Review software Thunder Trappist tree Upgrade Weather Westvleteren Westvleteren 12 Wordpress Youtube
  • Copyright notice

    Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
    Creative Commons Licentie
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
© Inparablog. Proudly Powered by WordPress | Nest Theme by YChong