Unexpected initial null in return from split()

C

Craig M. Votava

Folks-

I'm trying to do something fairly that should be
fairly easy, and obvious:
------------------------------------------------
my %info = split(/magic/, `pkginfo -l SUNWarc`);
print $info{VERSION};
------------------------------------------------
My problem is: what magic will make this work?

Here's my closest solution (using arrays for debugging):
---------------------------------------------------------
use Data::Dumper;
my @info = split(/^\s+(\S+:)\s+/m, `pkginfo -l SUNWarc`);
print STDERR Dumper(\@info);
---------------------------------------------------------

In my environment, info[0] is null, all the rest of the
array is exactly what I would expect. WHERE IS THIS INITIAL
NULL COMING FROM!!! Grrrr, I'm frustrated.

Any help is very much appreciated!

Thanks

-Craig Votava
Lucent Technologies
(e-mail address removed)
 
B

Brian McCauley

Craig M. Votava said:
Folks-

I'm trying to do something fairly that should be
fairly easy, and obvious:

You don't say what the output of "pkginfo -l SUNWarc" looks
like, nor what %info should look like.

Random shot-in-the-dark you want to achive with split what most people
would do with a simple match:

my %info = map { /^\s*(\S+):\s+(.*)/ } `pkginfo -l SUNWarc`;

Or.

my %info = `pkginfo -l SUNWarc` =~ /^\s*(\S+):\s+(.*)/mg;
Here's my closest solution (using arrays for debugging):
my @info = split(/^\s+(\S+:)\s+/m, `pkginfo -l SUNWarc`);
In my environment, info[0] is null, all the rest of the
array is exactly what I would expect.

Really? I would have though that that would have had 'VERSION:' where
you wanted 'VERSION'. :)
WHERE IS THIS INITIAL NULL COMING FROM!!! Grrrr, I'm frustrated.

What split does is _split_ the string using the pattern as a
_delimiter_. The first element of the returned list is the bit
_before_ the first place the pattern matches. If the first match is
at the start of the string then the first element of the returned list
will be an empty string.

I can see no obvious way to avoid this. If you insist on using split
you'll have to discard the empty string afterwards.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
M

Malcolm Dew-Jones

Craig M. Votava ([email protected]) wrote:
: Folks-

: I'm trying to do something fairly that should be
: fairly easy, and obvious:
: ------------------------------------------------
: my %info = split(/magic/, `pkginfo -l SUNWarc`);
: print $info{VERSION};
: ------------------------------------------------
: My problem is: what magic will make this work?

: Here's my closest solution (using arrays for debugging):
: ---------------------------------------------------------
: use Data::Dumper;
: my @info = split(/^\s+(\S+:)\s+/m, `pkginfo -l SUNWarc`);
: print STDERR Dumper(\@info);
: ---------------------------------------------------------

: In my environment, info[0] is null, all the rest of the
: array is exactly what I would expect. WHERE IS THIS INITIAL
: NULL COMING FROM!!! Grrrr, I'm frustrated.

Does this make it clearer

$_ = ",abc,123"; # three comma seperated items

my @a = split /,/ ; # split on the commas

for (my $i=0; $i < @a; $i++ )
{
print "$i:=>$a[$i]<=\n"; # see what each item is
}
 
G

Glenn Jackman

Craig M. Votava said:
Folks-

I'm trying to do something fairly that should be
fairly easy, and obvious:

Rule of thumb: use split if you know what to throw away, use regular
expression if you know what to keep.

perl -le '
my %info = `pkginfo -l SUNWarc` =~ m{
([A-Z]+):\s+ # the key (assuming all upper case only)
(.*?)\s* # the value (non-greedy)
(?=\Z|[A-Z]+:) # lookahead for end of string or next label
}xsg;
use Data::Dumper; print Dumper(\%info);
'
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top