awk's NF in Perl

Discussion in 'Perl' started by Thorsten Gottschalk, Mar 5, 2004.

  1. Hi all,

    I want to have the awk NF variable in Perl. Yes I know there are many
    hints for that. But I only found the folloing:

    #!/usr/bin/perl

    $ln="1;2;3;4;;;;;;10";
    $nf = @records = split(/;/, $ln);
    print "NF:$nf\n";
    $"=";";
    print "@records\n";

    output:
    NF:10
    1;2;3;4;;;;;;10

    so it looks good, now I change the script a little bit, look here:

    # change this line in script:
    $ln="1;2;3;4;;;;;;";

    output:
    NF:4
    1;2;3;4

    So now you can see the trick with spilt did not work any time. Are
    there any other suggestions out there?

    ciao
    Thorsten
     
    Thorsten Gottschalk, Mar 5, 2004
    #1
    1. Advertising

  2. Thorsten Gottschalk

    Jim Gibson Guest

    In article <>, Thorsten
    Gottschalk <> wrote:

    > Hi all,
    >
    > I want to have the awk NF variable in Perl. Yes I know there are many
    > hints for that. But I only found the folloing:
    >
    > #!/usr/bin/perl


    use strcit:
    use warnings;

    >
    > $ln="1;2;3;4;;;;;;10";


    my $ln = "1;2;3;4;;;;;;;";

    > $nf = @records = split(/;/, $ln);


    my $nf = my @records = split(/;/, $ln, -1 );

    > print "NF:$nf\n";
    > $"=";";
    > print "@records\n";
    >
    > output:
    > NF:10
    > 1;2;3;4;;;;;;10
    >
    > so it looks good, now I change the script a little bit, look here:
    >
    > # change this line in script:
    > $ln="1;2;3;4;;;;;;";
    >
    > output:
    > NF:4
    > 1;2;3;4


    NF:10
    1;2;3;4;;;;;;;

    >
    > So now you can see the trick with spilt did not work any time. Are
    > there any other suggestions out there?
    >
    > ciao
    > Thorsten


    Check the documentation on split:

    perldoc -f split

    FYI: this newgroup is defunct. Try comp.lang.perl.misc in the future.
     
    Jim Gibson, Mar 5, 2004
    #2
    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. NNTP

    sed awk or perl for this?

    NNTP, Sep 11, 2003, in forum: Perl
    Replies:
    13
    Views:
    3,479
    Alan Connor
    Sep 30, 2003
  2. gorda
    Replies:
    2
    Views:
    544
    Andrew Shitov
    Oct 21, 2003
  3. NNTP
    Replies:
    2
    Views:
    949
    rakesh sharma
    Apr 7, 2004
  4. engineer

    Tool for perl script to awk

    engineer, Jun 14, 2004, in forum: Perl
    Replies:
    1
    Views:
    881
    Joe Smith
    Jun 14, 2004
  5. bolega
    Replies:
    6
    Views:
    2,054
Loading...

Share This Page