Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string into array in Perl

my $line = "file1.gz file2.gz file3.gz";
my @abc = split('', $line);
print "@abc\n";

Expected output:

file1.gz
file2.gz
file3.gz

I want the output to be file1.gz in $abc[0], file2.gz in $abc[1], and file3.gz in $abc[2]. How do I split $line?

like image 996
Rahul Reddy Avatar asked Jun 01 '13 11:06

Rahul Reddy


People also ask

How do I convert a string to an array in Perl?

A string can be converted into an array using the split() function. @ARRAY = split (/REGEX/, $STRING);

How do I split a string in Perl?

split() is a string function in Perl which is used to split or you can say to cut a string into smaller sections or pieces. There are different criteria to split a string, like on a single character, a regular expression(pattern), a group of characters or on undefined value etc..

How do I split a string into multiple strings in Perl?

A string is splitted based on delimiter specified by pattern. By default, it whitespace is assumed as delimiter. split syntax is: Split /pattern/, variableName.

What is $_ in Perl?

The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }


1 Answers

Splitting a string by whitespace is very simple:

print $_, "\n" for split ' ', 'file1.gz file1.gz file3.gz';

This is a special form of split actually (as this function usually takes patterns instead of strings):

As another special case, split emulates the default behavior of the command line tool awk when the PATTERN is either omitted or a literal string composed of a single space character (such as ' ' or "\x20"). In this case, any leading whitespace in EXPR is removed before splitting occurs, and the PATTERN is instead treated as if it were /\s+/; in particular, this means that any contiguous whitespace (not just a single space character) is used as a separator.


Here's an answer for the original question (with a simple string without any whitespace):

Perhaps you want to split on .gz extension:

my $line = "file1.gzfile1.gzfile3.gz";
my @abc = split /(?<=\.gz)/, $line;
print $_, "\n" for @abc;

Here I used (?<=...) construct, which is look-behind assertion, basically making split at each point in the line preceded by .gz substring.

If you work with the fixed set of extensions, you can extend the pattern to include them all:

my $line = "file1.gzfile2.txtfile2.gzfile3.xls";
my @exts = ('txt', 'xls', 'gz');
my $patt = join '|', map { '(?<=\.' . $_ . ')' } @exts;
my @abc = split /$patt/, $line;
print $_, "\n" for @abc;
like image 146
raina77ow Avatar answered Oct 12 '22 05:10

raina77ow