Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Kevin Walzer
December 1, 2012, 6:17 pm
rate this thread
It's actually supposed to be a multiline file with a date in yyyy-mm-dd
format as the first field on each line. To correctly structure the data,
I'm trying to insert insert a newline character before all date fields
and and then split the file into an array on the newline char. The issue
I'm running into is that I can't get the substitution pattern right.
Here's my code:
$_ =~ s/$date1/$date2/g;
This pattern appears to correctly match the date field, but rather
simply inserting a newline in front of the date field's data it inserts
a newline and then a string literal that looks like this:
What I want is to replace 2012-01-01 with \n2012-01-01, and all
subsequent date fields in the file in the same fashion. Can someone
suggest a better way to do this?
Code by Kevin
Re: Insert newline before date in yyyy-mm-dd format
If you just want to split the text into an array, you don't need to
insert newlines first. Just split on a zero-width assertion:
my @lines = split /(?=[0-9]-[0-9]-[0-9])/;
That (?=) matches the gap between two characters immediately before a