All:
While tracking down an interesting bug in some of my code, I discovered that while chomp can take a list such as an array, it doesn't work quite right on an AoA. Consider the following:

#!/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


In reply to Chomp an AoA? by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.