Search Tab-delimited file for Null fields
Thanks to everyone for their help on this problem. I finally ended up using
the following:
#!perl
use warnings;
use strict;
my [at] empty = ();
my [at] headings = ();
my $sum;
open INDEX, "> fieldcontents.txt" or die "can't create fieldcontents.txt
$!";
open INPUT, "<lv1.txt" or die "can't open data file: $!";
{
while (<INPUT>) {
if ($. == 1) {
chomp; # remove newline
[at] headings = split /\t/; # get each of the headings
[at] empty = [at] headings;
}
next if $. == 1 ;
chomp; # remove newline
my [at] fields = split /\t/; # get each of the fields
for (my $i = 0; $i <= $#fields; $i++) {
if (length($fields[$i]) >= 1) { #look for fields with no
value
$empty[$i] = ""; #assign flag to new array
}
}
}
foreach ( [at] empty){
if( $_ ){
print INPUT "$_\n";
}
}
close INPUT;
}
close INDEX;
This does what I want for now but I'm still learning so I'm sure I'll be
adding to it. Please ignore my second post to the list -- I was feeling
kind of brain dead at the time.
Thanks,
Debbie
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Re: Search Tab-delimited file for Null fields
Debbie Cooper wrote:
> Thanks to everyone for their help on this problem. I finally ended up using
> the following:
> #!perl
> use warnings;
> use strict;
>
> my [at] empty = ();
> my [at] headings = ();
Aggregate variables created with my() are empty by default so assigning an
empty list to them is redundant.
> my $sum;
>
> open INDEX, "> fieldcontents.txt" or die "can't create fieldcontents.txt
> $!";
> open INPUT, "<lv1.txt" or die "can't open data file: $!";
> {
>
> while (<INPUT>) {
> if ($. == 1) {
> chomp; # remove newline
> [at] headings = split /\t/; # get each of the headings
> [at] empty = [at] headings;
> }
> next if $. == 1 ;
> chomp; # remove newline
There is no need to chomp() or compare $. twice.
while (<INPUT>) {
chomp; # remove newline
if ($. == 1) {
[at] headings = split /\t/; # get each of the headings
[at] empty = [at] headings;
next;
}
> my [at] fields = split /\t/; # get each of the fields
>
> for (my $i = 0; $i <= $#fields; $i++) {
The usual Perl way to write that is:
for my $i ( 0 .. $#fields ) {
> if (length($fields[$i]) >= 1) { #look for fields with no
> value
>
> $empty[$i] = ""; #assign flag to new array
> }
> }
> }
>
> foreach ( [at] empty){
> if( $_ ){
> print INPUT "$_\n";
> }
> }
>
> close INPUT;
> }
>
> close INDEX;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
RE: Search Tab-delimited file for Null fields
You probably don't appreciate the fact that I'm including the prior message
in this latest email. But I want to let you all know how much stuff like
this helps someone like me who's learning Perl. To understand how my code
could be more efficient is a great learning tool. I can look up things in
books all day but I usually won't know what is the "best" or "better" way to
do something from that. Thank you for taking the time to go through my code
and comment on the pieces that weren't needed and, even better, to tell me
why they weren't needed.
I'm liking this Perl stuff more and more.
Thanks,
Debbie
-----Original Message-----
From: John W. Krahn [mailto:krahnj [at] telus.net]
Sent: Tuesday, November 30, 2004 4:40 PM
To: Perl Beginners
Subject: Re: Search Tab-delimited file for Null fields
Debbie Cooper wrote:
> Thanks to everyone for their help on this problem. I finally ended up
using
> the following:
> #!perl
> use warnings;
> use strict;
>
> my [at] empty = ();
> my [at] headings = ();
Aggregate variables created with my() are empty by default so assigning an
empty list to them is redundant.
> my $sum;
>
> open INDEX, "> fieldcontents.txt" or die "can't create fieldcontents.txt
> $!";
> open INPUT, "<lv1.txt" or die "can't open data file: $!";
> {
>
> while (<INPUT>) {
> if ($. == 1) {
> chomp; # remove newline
> [at] headings = split /\t/; # get each of the headings
> [at] empty = [at] headings;
> }
> next if $. == 1 ;
> chomp; # remove newline
There is no need to chomp() or compare $. twice.
while (<INPUT>) {
chomp; # remove newline
if ($. == 1) {
[at] headings = split /\t/; # get each of the headings
[at] empty = [at] headings;
next;
}
> my [at] fields = split /\t/; # get each of the fields
>
> for (my $i = 0; $i <= $#fields; $i++) {
The usual Perl way to write that is:
for my $i ( 0 .. $#fields ) {
> if (length($fields[$i]) >= 1) { #look for fields with no
> value
>
> $empty[$i] = ""; #assign flag to new array
> }
> }
> }
>
> foreach ( [at] empty){
> if( $_ ){
> print INPUT "$_\n";
> }
> }
>
> close INPUT;
> }
>
> close INDEX;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Re: Search Tab-delimited file for Null fields
John Krahn wrote:
> Debbie Cooper wrote:
> > my [at] empty = ();
> > my [at] headings = ();
>
> Aggregate variables created with my() are empty by default so assigning an
> empty list to them is redundant.
>
I hope I am not opening a can of worms by getting into a style
disucssion, and I will preface this entire thread with:
Style is a personal issue; pick one that works for you and be
consistent but not dogmatic. The style must serve you; you do not
serve the style. Read good code, and incorporate stylistic idiom
that suits the task at hand.
I used to be in the habit of always explicitly assigning to EVERY
variable I used upon declaration, and when declaring a variable that I
will use for a hashref or listref, I still do explicitly assign.
# instantiate a hasref to hold data about some person
my $person = {};
# instantiate a listref to hold my family
my $people = [];
$person->{hair} = 'blond';
$person->{name} = 'Lawrence';
$people->[0] = $person;
Of late, I have gotten out of the habit of explicitly initializing
lists or hashes, but I can see doing that piece of extraneous code to
reinforce in the users mind that the list/hash is starting out
virginal.
More often, I defer the declaration of the lexical variable until I
have the list or hash elements available, so I never declare them as
empty.
my $person = { hair => 'blond', name => 'Lawrence' } ;
my $people = [ $person ] ;
Obviously the solution that elides the intermediate $person variable
should be leaping to every reader's mind now.
my $people = [ { hair => 'blond', name => 'Lawrence } ] ;
But these are the stylistic differences that serve to distinguish the
masters from the aprentices.
One begins by teaching the novice the straightforward but verbose
technique, and later the shortcuts.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>