empty fields

Discussion in 'Perl Misc' started by Awkish, Sep 24, 2006.

  1. Awkish

    Awkish Guest

    Hi,
    I must parse files of this form:

    field1,,,field4,,field6,,,,,,


    That is, the fields of each line are separated by a comma, and the fields
    can be empty.

    With awk I can do this:

    awk -F, '{print $6; print NF}'

    with the above line I get:
    field6
    12
    and that's what I want


    How can I get the same result with Perl?
    I tried:

    perl -F, 'print $F[5];print $#F+1'

    which gives:
    field6
    5

    Perl drops the "empty" fields after the last non-empty field :-(
     
    Awkish, Sep 24, 2006
    #1
    1. Advertising

  2. On 2006-09-24, Awkish <> wrote:
    > Perl drops the "empty" fields after the last non-empty field :-(


    => perl -e 'use Data::Dumper; my $q = q{field1,,,field4,,field6,,,,,,}; my @w = split /,/, $q, -1; print Dumper(\@w);'
    $VAR1 = [
    'field1',
    '',
    '',
    'field4',
    '',
    'field6',
    '',
    '',
    '',
    '',
    '',
    ''
    ];

    depesz

    --
    mój bo¿e, spraw abym milcza³, dopóki siê nie upewniê, ¿e naprawdê mam
    co¶ do powiedzenia. (c) 1998 depesz
     
    hubert depesz lubaczewski, Sep 24, 2006
    #2
    1. Advertising

  3. Awkish wrote:
    > Hi,
    > I must parse files of this form:
    >
    > field1,,,field4,,field6,,,,,,
    >
    >
    > That is, the fields of each line are separated by a comma, and the fields
    > can be empty.
    >
    > With awk I can do this:
    >
    > awk -F, '{print $6; print NF}'
    >
    > with the above line I get:
    > field6
    > 12
    > and that's what I want
    >
    >
    > How can I get the same result with Perl?
    > I tried:
    >
    > perl -F, 'print $F[5];print $#F+1'
    >
    > which gives:
    > field6
    > 5
    >
    > Perl drops the "empty" fields after the last non-empty field :-(


    Yes, this is a feature of the default beaviour of split() but can be
    overidden by making the 3rd argument negative.

    AFAIK there's no way to do this with autosplit so you need to spell it
    out.

    perl -ne '@F=split/,/,$_,-1; print $F[5];print $#F+1'
     
    Brian McCauley, Sep 24, 2006
    #3
  4. Brian McCauley wrote:
    > perl -ne '@F=split/,/,$_,-1; print $F[5];print $#F+1'


    Oops, forgot the chomp! There probably should be a chomp too.

    perl -nle '@F=split/,/,$_,-1; print $F[5];print $#F+1'
     
    Brian McCauley, Sep 24, 2006
    #4
  5. Mumia W. (reading news) wrote:
    > #!/bin/sh
    >
    > echo 'field1,,,field4,,field6,,,,,,' | \
    > perl -wle '
    > while (<>) {
    > @F = split /,/, $_;
    > print $F[5];
    > print scalar @F;
    > }
    > '


    You forgot to chomp(). (That's what made me realise I'd done so too in
    another branch).

    That'll be OK if $F[5] is not the last element but if any line in the
    input has exactly 6 fields then you'll get an extra newline in the
    output.

    If you'd not forgotten to chomp() then you would have gotten away with
    omitting the -1 on the split().

    Note: If you use the -n switch with the -l switch Perl will insert the
    chomp for you.

    perl -wnle '
    @F = split /,/, $_, -1;
    print $F[5];
    print scalar @F;
    '
     
    Brian McCauley, Sep 24, 2006
    #5
  6. Brian McCauley wrote:

    > If you'd not forgotten to chomp() then you would have gotten away with
    > omitting the -1 on the split().


    That should, of course, say "wouldn't have gotten away".
     
    Brian McCauley, Sep 24, 2006
    #6
  7. Awkish

    Bob Walton Guest

    Awkish wrote:
    ....

    > With awk I can do this:
    >
    > awk -F, '{print $6; print NF}'
    >
    > with the above line I get:
    > field6
    > 12
    > and that's what I want
    >
    >
    > How can I get the same result with Perl?

    ....

    perldoc a2p

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Sep 24, 2006
    #7
  8. Awkish

    Eric Amick Guest

    On Sun, 24 Sep 2006 12:18:11 +0200, Awkish <> wrote:

    >Hi,
    >I must parse files of this form:
    >
    >field1,,,field4,,field6,,,,,,
    >
    >
    >That is, the fields of each line are separated by a comma, and the fields
    >can be empty.
    >
    >With awk I can do this:
    >
    >awk -F, '{print $6; print NF}'
    >
    >with the above line I get:
    >field6
    >12
    >and that's what I want
    >
    >
    >How can I get the same result with Perl?
    >I tried:
    >
    >perl -F, 'print $F[5];print $#F+1'


    According to the description in perlrun (ahem), you need

    perl -an -F, "print $F[5],$#F+1"

    I tried it. It works.
    --
    Eric Amick
    Columbia, MD
     
    Eric Amick, Sep 25, 2006
    #8
  9. Eric Amick wrote:
    > On Sun, 24 Sep 2006 12:18:11 +0200, Awkish <> wrote:
    >
    >>I must parse files of this form:
    >>
    >>field1,,,field4,,field6,,,,,,
    >>
    >>That is, the fields of each line are separated by a comma, and the fields
    >>can be empty.
    >>
    >>With awk I can do this:
    >>
    >>awk -F, '{print $6; print NF}'
    >>
    >>with the above line I get:
    >>field6
    >>12
    >>and that's what I want
    >>
    >>How can I get the same result with Perl?
    >>I tried:
    >>
    >>perl -F, 'print $F[5];print $#F+1'

    >
    > According to the description in perlrun (ahem), you need
    >
    > perl -an -F, "print $F[5],$#F+1"
    >
    > I tried it.


    Are you really, really, *really* sure that you tried it?


    > It works.


    Prove it.



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 25, 2006
    #9
  10. Awkish

    Awkish Guest

    Hi thanks for all your insights, you're very helpful.
     
    Awkish, Sep 25, 2006
    #10
  11. Awkish

    Eric Amick Guest

    On Mon, 25 Sep 2006 02:51:34 GMT, "John W. Krahn" <>
    wrote:

    >Eric Amick wrote:
    >> According to the description in perlrun (ahem), you need
    >>
    >> perl -an -F, "print $F[5],$#F+1"
    >>
    >> I tried it.

    >
    >Are you really, really, *really* sure that you tried it?
    >
    >
    >> It works.

    >
    >Prove it.


    Fine. I misread a semicolon as a comma. The basic point is still
    correct.
    --
    Eric Amick
    Columbia, MD
     
    Eric Amick, Sep 25, 2006
    #11
  12. Eric Amick wrote:
    > On Mon, 25 Sep 2006 02:51:34 GMT, "John W. Krahn" <>
    > wrote:
    >
    >>Eric Amick wrote:
    >>>According to the description in perlrun (ahem), you need
    >>>
    >>>perl -an -F, "print $F[5],$#F+1"
    >>>
    >>>I tried it.

    >>Are you really, really, *really* sure that you tried it?
    >>
    >>
    >>>It works.

    >>Prove it.

    >
    > Fine. I misread a semicolon as a comma.


    Huh?

    > The basic point is still correct.


    It has more wrong than just a semicolon or a comma.



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 26, 2006
    #12
    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. call_me_anything
    Replies:
    4
    Views:
    502
    Pete Becker
    Sep 30, 2007
  2. Cyril.Liu
    Replies:
    0
    Views:
    479
    Cyril.Liu
    Dec 2, 2008
  3. middletree

    how to copy from fields to other fields

    middletree, Nov 5, 2003, in forum: ASP General
    Replies:
    1
    Views:
    186
    middletree
    Nov 5, 2003
  4. Replies:
    0
    Views:
    312
  5. AMT2K5
    Replies:
    1
    Views:
    216
    Eric Schwartz
    Nov 8, 2005
Loading...

Share This Page