awk's NF in Perl

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thorsten Gottschalk

    awk's NF in Perl

    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
  • Jim Gibson

    #2
    Re: awk's NF in Perl

    In article <beb38f7.040305 0405.6c3988df@p osting.google.c om>, Thorsten
    Gottschalk <iqrity@web.d e> wrote:
    [color=blue]
    > 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[/color]

    use strcit:
    use warnings;
    [color=blue]
    >
    > $ln="1;2;3;4;;; ;;;10";[/color]

    my $ln = "1;2;3;4;;;;;;; ";
    [color=blue]
    > $nf = @records = split(/;/, $ln);[/color]

    my $nf = my @records = split(/;/, $ln, -1 );
    [color=blue]
    > 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[/color]

    NF:10
    1;2;3;4;;;;;;;
    [color=blue]
    >
    > So now you can see the trick with spilt did not work any time. Are
    > there any other suggestions out there?
    >
    > ciao
    > Thorsten[/color]

    Check the documentation on split:

    perldoc -f split

    FYI: this newgroup is defunct. Try comp.lang.perl. misc in the future.

    Comment

    Working...