Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > empty fields

Reply
Thread Tools

empty fields

 
 
Awkish
Guest
Posts: n/a
 
      09-24-2006
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
 
Reply With Quote
 
 
 
 
hubert depesz lubaczewski
Guest
Posts: n/a
 
      09-24-2006
On 2006-09-24, Awkish <(E-Mail Removed)> wrote:
> Perl drops the "empty" fields after the last non-empty field


=> perl -e 'use Data:umper; 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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      09-24-2006

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'

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-24-2006

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'

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-24-2006

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;
'

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-24-2006

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

 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      09-24-2006
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
 
Reply With Quote
 
Eric Amick
Guest
Posts: n/a
 
      09-25-2006
On Sun, 24 Sep 2006 12:18:11 +0200, Awkish <(E-Mail Removed)> 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      09-25-2006
Eric Amick wrote:
> On Sun, 24 Sep 2006 12:18:11 +0200, Awkish <(E-Mail Removed)> 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
 
Reply With Quote
 
Awkish
Guest
Posts: n/a
 
      09-25-2006
Hi thanks for all your insights, you're very helpful.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
epydoc: How to add new fields as the building fields? Cyril.Liu Python 0 12-02-2008 05:01 AM
print struct fields and its member structs' fields recursively, generically call_me_anything C++ 4 09-30-2007 10:12 PM
Newbie - Reading a file with delimited fields, storing first two fields in a hash AMT2K5 Perl Misc 1 11-08-2005 01:06 AM
how to copy from fields to other fields middletree ASP General 1 11-05-2003 06:24 PM
Netscape hidden fields - array - multiple fields with same name mark.reichman@rl.af.mil Javascript 0 07-17-2003 03:05 PM



Advertisments