Friday, 6 January 2017

Perl array modifications with splice, split & join

In this article I'll demonstrate modifying array contents using splice, split & join functions.
I've worked with one dimensional arrays for the sake of simplicity but the concepts would be valid for multidimensional arrays as well.

1.) Splice function:

This is used to remove a defined number of elements from an array beginning with a given offset & replacing the elements with other elements if specified.
The syntax is as follows:

splice @ARRAY, OFFSET [ , LENGTH [ , LIST ] ]

This function will remove the elements of @ARRAY designated by OFFSET and LENGTH and replaces them with LIST, if specified.


2.) Split function:

The split function is used to convert a set of strings into an array or contents of a file into a multidimentional array.
The syntax is as follows:

split [ pattern [ , expression [ , LIMIT ] ] ]

This function splits a string into an array. If LIMIT is specified the function splits into at most that number of fields.
If PATTERN is omitted, splits on whitespace.
I use the split function a lot for splitting files into indivudal columns.


3.) The join function:

The join function is somewhat the opposite of the split function. Like the split function splits a string into an array using a specified delimeter, the join function combines individual array elements into a string.
This also proves useful if you are trying to change a delimeter of contents in a file.

The syntax for join function is as follows:

join expression, list

This function joins the separate strings of LIST into a single string with fields separated by the value of expression and returns the string.


Here is a short script demonstrating the use of the above discussed functions:

[root@cent6 ~]# cat af.pl
#!/usr/bin/perl -w

use strict ;

##using splice function##

my @array = qw/linux solaris unix aix hpux/ ;

print "OS names before splicing: @array \n" ;

splice (@array, 0, 2, "debian", "ubuntu") ;

print "OS names after  splicing: @array \n" ;

##using split function##

my $var = "linux:solaris:unix:hp-ux:tru64" ;

my @string = split (':', $var) ;

#print each element on new line: foreach (@string) { print "$_ \n" ; }
print "@string \n" ;

##using join function##

my $var1 = "linux,solaris,unix,hp-ux,tru64" ;

my @str = split /,/, $var1 ;

my $jn = join '|', @str ;
print "$jn \n" ;

[root@cent6 ~]#

On execution the code yields the following output:

[root@cent6 ~]# ./af.pl
OS names before splicing: linux solaris unix aix hpux
OS names after  splicing: debian ubuntu unix aix hpux
linux solaris unix hp-ux tru64
linux|solaris|unix|hp-ux|tru64
[root@cent6 ~]#


There are many more functions associated with arrays available like push, pop, shift, unshift & sort. I might write articles describing the other functions at a later date.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...