QuinsUK
QuinsUK

Reputation: 371

String Splitting by letters in Perl

I am new to Perl and I am currently trying to split a string only on a couple of letter. I have looked at the other answers and they seem to be specific to that problem or there is lack of comments to understand the answer.

The ultimate goal is to split up a very long CSV file into receptive sections which can then be used later on. The sample data would is

HOME 123454 monkey lion 6.4.2.10 ( ABCD EFGH (Tue 20th August 2000) 12345 True )<br />HOME 123454 monkey lion 6.4.2.10 ( ABCD EFGH (Tue 20th August 2000) 12345 True )<br />

I would look to split the string by the "< br /> into there own strings which would then store the strings in an array. So far what I have tried to split the string is:

my $line1 = split("/<br />", $Line);

and testing it trying by printing out the output, but it doesn't work.

Upvotes: 0

Views: 486

Answers (2)

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74232

The split function returns the number of splits in scalar context. To get the list of splits, one needs to call split in list context:

my $str   = q{HOME 123454 monkey lion 6.4.2.10 ( ABCD EFGH (Tue 20th August 2000) 12345 True )<br />HOME 123454 monkey lion 6.4.2.10 ( ABCD EFGH (Tue 20th August 2000) 12345 True )<br />};
my @lines = split qr{<br\s?/>}, $str;

Upvotes: 7

BSen
BSen

Reputation: 187

$str = 'HOME 123454 monkey lion 6.4.2.10 ( ABCD EFGH (Tue 20th August 2000) 12345 True )<br />HOME 123454 monkey lion 6.4.2.10 ( ABCD EFGH (Tue 20th August 2000) 12345 True )<br />';

my @list = split(qr'<br />', $str);

say $_ for @list;

Upvotes: 3

Related Questions