Split string into array in Perl

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

Expected Result:

 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 ?

+8
source share
5 answers

Separating a string by space is very simple:

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

This is a special form of split in fact (since this function usually takes patterns instead of strings):

As another special case, split emulates the default behavior for the awk command-line tool when PATTERN either omitted or the letter string is a single space (for example, ' ' or "\x20" ). In this case, any leading spaces in EXPR removed before splitting, and PATTERN instead treated as if it were /\s+/ ; in particular, this means that any adjacent space (and not just one space) is used as a delimiter.


Here's the answer to the original question (with a simple line without spaces):

Maybe you want to split into .gz extension:

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

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

If you work with a fixed set of extensions, you can expand the template to include all of them:

 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; 
+15
source

With $line , as of now, you can simply split the line based on at least one space separator

 my @answer = split(' ', $line); # creates an @answer array 

then

 print("@answer\n"); # print array on one line 

or

 print("$_\n") for (@answer); # print each element on one line 

I prefer to use () for split , print and for .

+10
source

I found it very easy!

 my $line = "file1.gz file2.gz file3.gz"; my @abc = ($line =~ /(\w+[.]\w+)/g); print $abc[0],"\n"; print $abc[1],"\n"; print $abc[2],"\n"; 

output:

 file1.gz file2.gz file3.gz 

Here's a look at this tutorial to find more on Perl regex and scroll down to the More section.

-1
source

Just use / \ s + / against '' as a splitter. In this case, all the "extra" blanks were deleted. Usually this specific behavior is required. So in your case it will be:

 my $line = "file1.gz file1.gz file3.gz"; my @abc = split(/\s+/, $line); 
-2
source

You already have a few answers to your question, but I would like to add another minor question that might help add something.

To view data structures in Perl, you can use Data::Dumper . To print a line, you can use say , which adds the newline character "\n" after each call, rather than adding it explicitly.

I usually use \s , which matches the space character. If you add + , it will match one or more whitespace characters. You can read more about this here perlre .

 #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; my $line = "file1.gz file2.gz file3.gz"; my @abc = split /\s+/, $line; print Dumper \@abc; say for @abc; 
-2
source

Source: https://habr.com/ru/post/946310/


All Articles