empty fields

A

Awkish

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 :-(
 
H

hubert depesz lubaczewski

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
 
B

Brian McCauley

Awkish said:
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'
 
B

Brian McCauley

Brian said:
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'
 
B

Brian McCauley

Mumia said:
#!/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;
'
 
B

Brian McCauley

Brian said:
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".
 
B

Bob Walton

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
 
E

Eric Amick

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.
 
J

John W. Krahn

Eric said:
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
 

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,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top