awk's NF in Perl

  • Thread starter Thorsten Gottschalk
  • Start date
T

Thorsten Gottschalk

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
 
J

Jim Gibson

Thorsten said:
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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top