0

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.

2 Answers 2

7

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;
Sign up to request clarification or add additional context in comments.

Comments

3
$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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.