Looking ahead in a split operation.

S

Sandman

I am doing a tv listing kind of thing, and parsing a string that looks
something like this:

"12.00 Simpsons 12.30 Fresh prince 14.00 Superbowl"

Some of you might remember me asking for how to split that on the time yet keep
the delimiter. That worked out just fine. I am doing something like this now:

----
#!/usr/bin/perl
use strict;
use warnings;

my $string = "12.00 Simpsons 12.30 Fresh prince 14.00 Superbowl";

my @list = split / ?(?=\d\d\.\d\d)/, $string;

foreach (@list){
print "$_\n";
}
__END__
12.00 Simpsons
12.30 Fresh prince
14.00 Superbowl

----

Now, I realised that I wanted to display how long Simpsons would be on, so that
I would display "12.00-12.30 Simpsons".

Is there a way to do this without saving the individual starttimes and iterate
the data again, this time fetching the data from the saved times?

Basically, I would want to know, in iteration 1 what the delimiter was between
array item 1 and 2. I'm sure you understand what I am trying to do.

Thanks in advance!
 
A

Anno Siegel

Sandman said:
I am doing a tv listing kind of thing, and parsing a string that looks
something like this:

"12.00 Simpsons 12.30 Fresh prince 14.00 Superbowl"

Some of you might remember me asking for how to split that on the time yet keep
the delimiter. That worked out just fine. I am doing something like this now:

----
#!/usr/bin/perl
use strict;
use warnings;

my $string = "12.00 Simpsons 12.30 Fresh prince 14.00 Superbowl";

my @list = split / ?(?=\d\d\.\d\d)/, $string;

foreach (@list){
print "$_\n";
}
__END__
12.00 Simpsons
12.30 Fresh prince
14.00 Superbowl

$string =~ s/(\d\d\.\d\d) (.*?) (?=(\d\d\.\d\d))/$1-$3 $2\n/g;

And your lines are still too long.

Anno
 
G

Gunnar Hjalmarsson

Sandman said:
I am doing a tv listing kind of thing, and parsing a string that
looks something like this:

"12.00 Simpsons 12.30 Fresh prince 14.00 Superbowl"

Some of you might remember me asking for how to split that on the
time yet keep the delimiter. That worked out just fine.

12.00 Simpsons
12.30 Fresh prince
14.00 Superbowl

Now, I realised that I wanted to display how long Simpsons would be
on, so that I would display "12.00-12.30 Simpsons".

Is there a way to do this without saving the individual starttimes
and iterate the data again, this time fetching the data from the
saved times?

Why would you need to parse the data again, when all the info is in
the array?

Anyway, I think I'd drop the split() function and do something like
this instead:

my $string = '12.00 Simpsons 12.30 Fresh prince 14.00 Superbowl';
my ($elem, @list) = 0;
while ( $string =~ /(\d\d\.\d\d)\s+(.*?)\s*(?=\d\d\.\d\d|$)/g ) {
$list[$elem]{time} = $1;
$list[$elem]{title} = $2;
$elem ++;
}
for (0..$#list-1) {
print "$list[$_]{time}-$list[$_+1]{time} ",
"$list[$_]{title}\n";
}
print "$list[-1]{time}- $list[-1]{title}\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top