empty leading field from split()

D

DJ Stunks

I whipped this up for someone looking for a parser for the DATA listed.

It works well, but I can't figure out why the split() produces an empty
leading field... I ran it through re qw{ debug } but I couldn't pick
it up.

Can anyone shine some light on this for me?

TIA,
-jp

#!/usr/bin/perl

use strict;
use warnings;

my $text = do {local $/; <DATA>};

my %hash;
(undef, %hash) = split m{^ (.+?) \s* : \s* }xm, $text;

s/\s+/ /g and s/\s+$//g for values %hash; # repair whitespace

use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
print Dumper \%hash;

__DATA__
keyword1: blahblah
asdfas
keyword2: blah blah blah
more blah blah blah
keyword3: and so on
and on to in the end
three lines total
 
D

DJ Stunks

DJ said:
I can't figure out why the split() produces an empty leading field

oh man.

of course. as soon as I post, I realize. because it's a split and
there's no data before the first separator the first data field is
undef.......

sorry for the interruption, I should have thought for another half hour
before asking.

my bad.
-jp
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top