Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; my @Fields; my @Array; open (FILE,"file") or die "Can't open file\n"; while (<FILE>) { next if (/^#/); @fields = split "\t" , $_; next unless (@Fields == 7); push @Array , [ @Fields[0,3,6] ]; } foreach my $foo (@fields) { print "$foo->[2]\n" if ($foo->[2] != 3); }
You are confused as to why the code won't work (scratching head) - oh, I see my mistake - there is a trailing newline at the end - let me just get rid of it.
push @Array , [ @Fields[0,3,6] ]; } chomp @Array; foreach my $foo (@fields) { print "$foo->[2]\n" if ($foo->[2] != 3); }
Now it still doesn't work - this time you are getting some error about not being able to use a stringified array as a reference with strict enforced.
you remove the chomp as is and you put it back in the code that builds the array
while (<FILE>) { next if (/^#/); @fields = split "\t" , $_; next unless (@Fields == 7); chomp $Fields[6]; push @Array , [ @Fields[0,3,6] ]; }
You are happy your code is now working, but you are sad that you have to do it THAT way.
so friendly monks - is there a way to chomp an AoA similar to:
chomp @Array;
Thanks in advance - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Chomp an AoA?
by Gilimanjaro (Hermit) on Jan 28, 2003 at 20:49 UTC | |
by PodMaster (Abbot) on Jan 29, 2003 at 15:29 UTC | |
|
Re: Chomp an AoA?
by chromatic (Archbishop) on Jan 28, 2003 at 21:53 UTC | |
|
Re: Chomp an AoA?
by integral (Hermit) on Jan 28, 2003 at 19:16 UTC | |
|
Re: Chomp an AoA?
by dempa (Friar) on Jan 28, 2003 at 19:10 UTC | |
|
Re: Chomp an AoA?
by Aristotle (Chancellor) on Jan 31, 2003 at 03:18 UTC |