Unexpected initial null in return from split()

Discussion in 'Perl Misc' started by Craig M. Votava, Jan 30, 2004.

  1. 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
     
    Craig M. Votava, Jan 30, 2004
    #1
    1. Advertising

  2. "Craig M. Votava" <> writes:

    > 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?


    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\\
     
    Brian McCauley, Jan 30, 2004
    #2
    1. Advertising

  3. Craig M. Votava () 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
    }
     
    Malcolm Dew-Jones, Jan 30, 2004
    #3
  4. Craig M. Votava <> 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):
    > ---------------------------------------------------------
    > my @info = split(/^\s+(\S+:)\s+/m, `pkginfo -l SUNWarc`);


    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);
    '

    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Jan 30, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,966
    Mike Schilling
    Mar 29, 2006
  2. Carl
    Replies:
    21
    Views:
    1,009
    Patricia Shanahan
    Aug 24, 2006
  3. Francois Grieu
    Replies:
    14
    Views:
    822
    Micah Cowan
    Mar 7, 2008
  4. John Nagle
    Replies:
    10
    Views:
    421
    jhermann
    Aug 5, 2010
  5. Matthew Moss
    Replies:
    2
    Views:
    111
    Matthew Moss
    Mar 13, 2006
Loading...

Share This Page