rheaton has asked for the wisdom of the Perl Monks concerning the following question:

This question is a spin off of that was found in the last question I posted.

target_list is a hash I am using to determine my Unique Targets. If there is a better way of doing this please tell me. What am I doing wrong so that the only element that seems to be added to the @target_file array is the last element processed. The new pushs to the array are not adding the new @tfl_contents reference. Thanks again.

open(tfl_file, $target_file_layout) || die "Unable to open target +file layout metadata file $target_file_layout: $!\n"; foreach (<tfl_file>) { if($_ =~ /$ARGV[0]/){ chomp $_; @tfl_contents = split(/\&/, $_); if ($tfl_contents[9] ne ""){ $target_list{$tfl_contents[9]}++; push(@target_file, \@tfl_contents); } if ($debug eq "Y") { print "Source Found in Metadata: $_\n"; print "Contents Array = @tfl_contents\n"; print "Contents Ref = @$tfl_ref\n"; print "Element = $tfl_ref->[1]\n\n"; } } else{ #Do Nothing } } print Dumper(\@target_file); sleep; @tfl_list = keys %target_list; if ($debug eq "Y") { print "TARGET_LIST = @tfl_list\n"; } close(tfl_file);

Replies are listed 'Best First'.
Re: Adding Array References to an Array
by jasonk (Parson) on Dec 28, 2006 at 19:47 UTC

    use strict and declare your variables. Because @tfl_contents never goes out of scope, you are reusing the same array over and over again, and simply pushing a new reference to the same array into @target_file each time.


    We're not surrounded, we're in a target-rich environment!

      Good catch.

      You might have mentioned to the OP, that the/a/one remedy in this situation is to construct a new reference on every push, as in

      push(@target_file, [@tfl_contents]);

Re: Adding Array References to an Array
by swampyankee (Parson) on Dec 28, 2006 at 19:34 UTC

    It's going to be a tad difficult to figure out what's going on without enough data (a sample of the input file, say). It may well be that you're getting 1 element because that's all there is.

    There are some stylistic issues with your code:

    • $_ =~ /$ARGV[0]/ is equivalent to the more usual /$ARGV[0]/. If $ARGV[0] can contain metacharacters (backslash, periods, etc), it probably should be quoted.
    • if($tfl_contents[9] ne "") (you did have a null string, no?) may be better written as if($tfl_contents[9]).

    added in update

    As chromatic notes, in his response, replacing

    if($tfl_contents[9] ne "")

    with

    if($tfl_contents[9])

    may also be worse, e.g. if there's a 0 in that location.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
      if($tfl_contents[] ne "") (you did have a null string, no?) may be better written as if($tfl_contents[9]).

      It may be worse, too, if there's a 0 in that slot.