views:

71

answers:

2

I got a problem when I parse a complex file which include thousands of lines.

I already implemented my Perl script like this days ago.

 my ($head, $tail) = split /=/, $line;

Nearly all my source file $line style as below:

constant normalLines = <type value>     /*  hello world  */

and I can get the output $tail = /* hello world */

Today I found a bug when I parse the line like this (there are two = in the line)

constant specialLine = <type value>     /*  hello = world  */

But now the output is $tail = /* hello

How can I fix my bug still using split() in my code above? I still want the output $tail = /* hello = world */

+7  A: 

You can specify the limit parameter to tell split how many parts you want at most:

# split /PATTERN/,EXPR,LIMIT

my ($head, $tail) = split /=/, $line, 2;
Thilo
@Thilo. That's great. I don't know how to describe my question better actually. But you input what i need really. I wish to mark it as my answer right now. But I must waiting another 6 min. haha. Thanks a lot.
Nano HE
Whenever you have a question about a Perl function, read its documentation. This answer is already in there. :)
brian d foy
+2  A: 

@Thilo is exactly right about how you can fix this, but the source of the problem is that you were doing a list assignment in a way that caused list items to be dropped. Doing the split like you had would result in the following list:

  ["constant specialLine ", " <type value>     /*  hello ", " world  */"]

When you use that in a list assignment, you take the first two values and the rest are thrown away.

Daenyth
@Daenyth. Thanks for your input. That's why I love SOF. Sometime I can receive some nice comments even I already got my answer and get more deeply in my original question here. BTW. I learned a new english word *Nerd*. ;)
Nano HE