Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] Printing hash array references

Expand Messages
  • merlyn@stonehenge.com
    ... hooyar66 I would also like to know why (it appears) that I need to use hooyar66 main:: hooyar66 so often - initially I did not require it, but after
    Message 1 of 5 , Apr 5, 2005
      >>>>> "hooyar66" == hooyar66 <neville.hodder@...> writes:

      hooyar66> I would also like to know why (it appears) that I need to use
      hooyar66> "main::"
      hooyar66> so often - initially I did not require it, but after creating my
      hooyar66> functions now certain lines - peculiarly even within main - require
      hooyar66> it.

      That's definitely a sign you're doing something wrong. Go back
      to never saying that, and see if you can resolve your problem another way.

      %main::foo is *not* the same as %foo when "my %foo" has been seen earlier.
      And yet %main::foo bypasses the "use strict" mechanism.

      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
      Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
      See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    • hooyar66
      Group Please find my program code below and then some sample data below that. I posted this query last week and have tried to make this easier to understand. I
      Message 2 of 5 , Apr 12, 2005
        Group

        Please find my program code below and then some sample data below
        that.

        I posted this query last week and have tried to make this easier to
        understand. I have removed all the previous (unnecessary) references
        to main::

        The problem that I am having is illustrated in two sections:
        The first section begins: "#Print current hash key (reference)" which
        works as expected, and prints the reference to a hash array.

        The next section commented "#Print first element of current hash key"
        ought to print out the first element of the 'current' hash array - but
        it never finds anything defined.
        Q1. Why is this not finding an array element value?

        I have had to comment out the actual code for the "element" section
        because if this is interpreted the following 'foreach' loop also fails
        Q2. Why does my faulty code effect the 'foreach' loop?

        It is almost as if just by referring to the hash array element, the
        entire hash and values are deleted.

        Thanks for any ideas
        NJH

        ---------------------------------------------

        #! perl

        use strict;
        use warnings;
        use diagnostics;

        my ($line, %symbol_data, @current_symbol, $count);

        $count = 0;

        while ($line = <>){
        chomp($line);
        #Change separator
        my @fields = split /!/, $line;
        #Next line if current starts with A or J
        # next if (($fields[0] =~ /(^A|^J)/)||($fields[1] eq "CHECKED"));
        next if ($fields[0] =~ /(^A|^J)/);

        #Process line
        my @status = &parse_line(@fields);

        #Save symbol array to hash
        if(($status[0] eq "new")&&($status[1] eq "dims")){&new_sym($fields[2]
        )}

        #Print current hash key (reference)
        if (exists $symbol_data{$fields[2]}){
        print "Key)", $symbol_data{$fields[2]}, "\n";
        }
        #Print first element of current hash key
        # if (exists $symbol_data{$fields[2]}->[0]){
        # print "Element)", $symbol_data{$fields[2]}->[0], "\n";
        # }


        }

        foreach my $key (sort keys %symbol_data) {
        $count = 0;
        while(exists $symbol_data{$key}->[$count]){
        print "[$count] is: ", $symbol_data{$key}->[$count], "\n";
        $count++;
        };
        print "\n";
        }

        ####################
        sub parse_line{

        my @results = ("empty", "empty", "empty");

        #Check for new symbol
        if($symbol_data{$_[2]}){$results[0] = "exists"}
        else{$results[0] = "new"}

        #Numeric (dimension) data found
        if($_[3] =~ /^\d/){$results[1] = "dims"}

        #Add new data to array
        if(($results[0] eq "new")&&($results[1] ne "dims")){
        $current_symbol[$count] = $_[3];
        $count++;
        $results[2]= "saved";
        }

        return @results;
        }

        ####################
        sub new_sym{

        #Finalise new symbol
        #Add a quantity element to @current_symbol;
        push @current_symbol, 1;
        @{$symbol_data{$_[0]}} = @current_symbol;
        @current_symbol = ();
        $count = 0;
        }

        ####################

        =comment
        #test data:

        A!STATUS!SYM_NAME!GRAPHIC_DATA_7!
        J!C:\Perl_dump\extract_process\test.brd!Thu Mar 10 11:05:41 2005!-75.
        000!-170.000!425.000!190.000!0.001!millimeters!TOP_LEVEL!31.496063
        mil!2!UP TO DATE!
        S!CHECKED!CONN_UMP_3MM_SMT!sym.req:0554!
        S!CHECKED!CONN_UMP_3MM_SMT!lib.name:conn_ump_3mm_smt!
        S!CHECKED!CONN_UMP_3MM_SMT!height:3.00mm!
        S!CHECKED!CONN_UMP_3MM_SMT!used:01ATiss1/Whitney RF!
        S!CHECKED!CONN_UMP_3MM_SMT!rev:2,NJH,26/05/04!
        S!CHECKED!CONN_UMP_3MM_SMT!2.325!
        S!CHECKED!CONN_UMP_3MM_SMT!1.200!
        S!CHECKED!CONN_UMP_3MM_SMT!4.300!
        S!CHECKED!CONN_UMP_3MM_SMT!5.900!
        S!CHECKED!CONN_UMP_3MM_SMT!sym.req:0554!
        S!CHECKED!CONN_UMP_3MM_SMT!lib.name:conn_ump_3mm_smt!
        S!CHECKED!CONN_UMP_3MM_SMT!height:3.00mm!
        S!CHECKED!CONN_UMP_3MM_SMT!used:01ATiss1/Whitney RF!
        S!CHECKED!CONN_UMP_3MM_SMT!rev:2,NJH,26/05/04!
        S!CHECKED!CONN_UMP_3MM_SMT!2.325!
        S!CHECKED!CONN_UMP_3MM_SMT!1.200!
        S!CHECKED!CONN_UMP_3MM_SMT!4.300!
        S!CHECKED!CONN_UMP_3MM_SMT!5.900!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!CAP_CHIP_0402!sym.req:0625!
        S!CHECKED!CAP_CHIP_0402!lib.name:cap_chip_0402!
        S!CHECKED!CAP_CHIP_0402!height:0.55mm!
        S!CHECKED!CAP_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!CAP_CHIP_0402!rev:3,NJH,08/07/04!
        S!CHECKED!CAP_CHIP_0402!1.000!
        S!CHECKED!CAP_CHIP_0402!0.500!
        S!CHECKED!CAP_CHIP_0402!1.500!
        S!CHECKED!RES_CHIP_0402!sym.req:0654!
        S!CHECKED!RES_CHIP_0402!lib.name:res_chip_0402!
        S!CHECKED!RES_CHIP_0402!height:0.40mm!
        S!CHECKED!RES_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!RES_CHIP_0402!rev:2,NJH,15/07/04!
        S!CHECKED!RES_CHIP_0402!1.000!
        S!CHECKED!RES_CHIP_0402!0.600!
        S!CHECKED!RES_CHIP_0402!1.500!
        S!CHECKED!RES_CHIP_0402!sym.req:0654!
        S!CHECKED!RES_CHIP_0402!lib.name:res_chip_0402!
        S!CHECKED!RES_CHIP_0402!height:0.40mm!
        S!CHECKED!RES_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!RES_CHIP_0402!rev:2,NJH,15/07/04!
        S!CHECKED!RES_CHIP_0402!1.000!
        S!CHECKED!RES_CHIP_0402!0.600!
        S!CHECKED!RES_CHIP_0402!1.500!
        S!CHECKED!RES_CHIP_0402!sym.req:0654!
        S!CHECKED!RES_CHIP_0402!lib.name:res_chip_0402!
        S!CHECKED!RES_CHIP_0402!height:0.40mm!
        S!CHECKED!RES_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!RES_CHIP_0402!rev:2,NJH,15/07/04!
        S!CHECKED!RES_CHIP_0402!1.000!
        S!CHECKED!RES_CHIP_0402!0.600!
        S!CHECKED!RES_CHIP_0402!1.500!
        S!CHECKED!RES_CHIP_0402!sym.req:0654!
        S!CHECKED!RES_CHIP_0402!lib.name:res_chip_0402!
        S!CHECKED!RES_CHIP_0402!height:0.40mm!
        S!CHECKED!RES_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!RES_CHIP_0402!rev:2,NJH,15/07/04!
        S!CHECKED!RES_CHIP_0402!1.000!
        S!CHECKED!RES_CHIP_0402!0.600!
        S!CHECKED!RES_CHIP_0402!1.500!
        S!CHECKED!RES_CHIP_0402!sym.req:0654!
        S!CHECKED!RES_CHIP_0402!lib.name:res_chip_0402!
        S!CHECKED!RES_CHIP_0402!height:0.40mm!
        S!CHECKED!RES_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!RES_CHIP_0402!rev:2,NJH,15/07/04!
        S!CHECKED!RES_CHIP_0402!1.000!
        S!CHECKED!RES_CHIP_0402!0.600!
        S!CHECKED!RES_CHIP_0402!1.500!
        S!CHECKED!RES_CHIP_0402!sym.req:0654!
        S!CHECKED!RES_CHIP_0402!lib.name:res_chip_0402!
        S!CHECKED!RES_CHIP_0402!height:0.40mm!
        S!CHECKED!RES_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!RES_CHIP_0402!rev:2,NJH,15/07/04!
        S!CHECKED!RES_CHIP_0402!1.000!
        S!CHECKED!RES_CHIP_0402!0.600!
        S!CHECKED!RES_CHIP_0402!1.500!
        S!CHECKED!RES_CHIP_0402!sym.req:0654!
        S!CHECKED!RES_CHIP_0402!lib.name:res_chip_0402!
        S!CHECKED!RES_CHIP_0402!height:0.40mm!
        S!CHECKED!RES_CHIP_0402!used:00ZRiss3/SopranoFM!
        S!CHECKED!RES_CHIP_0402!rev:2,NJH,15/07/04!
        S!CHECKED!RES_CHIP_0402!1.000!
        S!CHECKED!RES_CHIP_0402!0.600!
        S!CHECKED!RES_CHIP_0402!1.500!
        S!CHECKED!IND_0603!rev:3,NJH.26/05/04!
        S!CHECKED!IND_0603!used:01ATiss1/WhitneyRF!
        S!CHECKED!IND_0603!height:0.95mm!
        S!CHECKED!IND_0603!lib:ind_0603!
        S!CHECKED!IND_0603!sym.req:0569!
        S!CHECKED!IND_0603!1.400!
        S!CHECKED!IND_0603!0.950!
        S!CHECKED!IND_0603!1.750!
        S!CHECKED!IND_0603!rev:3,NJH.26/05/04!
        S!CHECKED!IND_0603!used:01ATiss1/WhitneyRF!
        S!CHECKED!IND_0603!height:0.95mm!
        S!CHECKED!IND_0603!lib:ind_0603!
        S!CHECKED!IND_0603!sym.req:0569!
        S!CHECKED!IND_0603!1.400!
        S!CHECKED!IND_0603!0.950!
        S!CHECKED!IND_0603!1.750!
        S!CHECKED!IND_0603!rev:3,NJH.26/05/04!
        S!CHECKED!IND_0603!used:01ATiss1/WhitneyRF!
        S!CHECKED!IND_0603!height:0.95mm!
        S!CHECKED!IND_0603!lib:ind_0603!
        S!CHECKED!IND_0603!sym.req:0569!
        S!CHECKED!IND_0603!1.400!
        S!CHECKED!IND_0603!0.950!
        S!CHECKED!IND_0603!1.750!
        S!CHECKED!IND_0603!rev:3,NJH.26/05/04!
        S!CHECKED!IND_0603!used:01ATiss1/WhitneyRF!
        S!CHECKED!IND_0603!height:0.95mm!
        S!CHECKED!IND_0603!lib:ind_0603!
        S!CHECKED!IND_0603!sym.req:0569!
        S!CHECKED!IND_0603!1.400!
        S!CHECKED!IND_0603!0.950!
        S!CHECKED!IND_0603!1.750!
        S!CHECKED!MTG_2_00CP1_20!sym.req:0759!
        S!CHECKED!MTG_2_00CP1_20!lib.name:mtg_2_00cp1_20!
        S!CHECKED!MTG_2_00CP1_20!height:0.00mm!
        S!CHECKED!MTG_2_00CP1_20!used:01BOiss1/Battery Pack!
        S!CHECKED!MTG_2_00CP1_20!rev:1,NJH,22/12/04!
        S!CHECKED!MTG_2_00CP1_20!2.000!
        S!CHECKED!MTG_2_00CP1_20!sym.req:0759!
        S!CHECKED!MTG_2_00CP1_20!lib.name:mtg_2_00cp1_20!
        S!CHECKED!MTG_2_00CP1_20!height:0.00mm!
        S!CHECKED!MTG_2_00CP1_20!used:01BOiss1/Battery Pack!
        S!CHECKED!MTG_2_00CP1_20!rev:1,NJH,22/12/04!
        S!CHECKED!MTG_2_00CP1_20!2.000!
        S!UNCHECKED!MTG_2_40CP1_70!sym.req:0701!
        S!UNCHECKED!MTG_2_40CP1_70!lib.name:MTG_2_40CP1_70!
        S!UNCHECKED!MTG_2_40CP1_70!height:0.00mm!
        S!UNCHECKED!MTG_2_40CP1_70!used:01BAiss2/USB programmer!
        S!UNCHECKED!MTG_2_40CP1_70!rev:1,NJH,28/09/04!
        S!UNCHECKED!MTG_2_40CP1_70!1.700!
        S!UNCHECKED!MTG_2_40CP1_70!2.400!
        S!UNCHECKED!MTG_2_40CP1_70!sym.req:0701!
        S!UNCHECKED!MTG_2_40CP1_70!lib.name:MTG_2_40CP1_70!
        S!UNCHECKED!MTG_2_40CP1_70!height:0.00mm!
        S!UNCHECKED!MTG_2_40CP1_70!used:01BAiss2/USB programmer!
        S!UNCHECKED!MTG_2_40CP1_70!rev:1,NJH,28/09/04!
        S!UNCHECKED!MTG_2_40CP1_70!1.700!
        S!UNCHECKED!MTG_2_40CP1_70!2.400!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!sym.req:0775!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!lib.name:ic_dip4_1r30_1c90_smt!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!height:1.10mm!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!used:01BZiss1/L-band antenna!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!rev:1,NJH,18/01/05!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!0.650!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!2.200!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!1.350!
        S!UNCHECKED!IC_DIP4_1R30_1C90_SMT!1.900!
        S!CHECKED!FIDUCIAL!sym.req:0504!
        S!CHECKED!FIDUCIAL!lib.name:fiducial!
        S!CHECKED!FIDUCIAL!height:0.00mm!
        S!CHECKED!FIDUCIAL!used:00YWiss3/GenesisMain!
        S!CHECKED!FIDUCIAL!rev:2,NJH,14/01/04!
        S!CHECKED!FIDUCIAL!1.250!
        S!CHECKED!FIDUCIAL!sym.req:0504!
        S!CHECKED!FIDUCIAL!lib.name:fiducial!
        S!CHECKED!FIDUCIAL!height:0.00mm!
        S!CHECKED!FIDUCIAL!used:00YWiss3/GenesisMain!
        S!CHECKED!FIDUCIAL!rev:2,NJH,14/01/04!
        S!CHECKED!FIDUCIAL!1.250!
        =cut




        --- In perl-beginner@yahoogroups.com, merlyn@s... wrote:
        > >>>>> "hooyar66" == hooyar66 <neville.hodder@p...> writes:
        >
        > hooyar66> I would also like to know why (it appears) that I need to
        use
        > hooyar66> "main::"
        > hooyar66> so often - initially I did not require it, but after
        creating my
        > hooyar66> functions now certain lines - peculiarly even within main
        - require
        > hooyar66> it.
        >
        > That's definitely a sign you're doing something wrong. Go back
        > to never saying that, and see if you can resolve your problem
        another way.
        >
        > %main::foo is *not* the same as %foo when "my %foo" has been seen
        earlier.
        > And yet %main::foo bypasses the "use strict" mechanism.
        >
        > --
        > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503
        777 0095
        > <merlyn@s...> <URL:http://www.stonehenge.com/merlyn/>
        > Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        > See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl
        training!
      • Damien Carbery
        ... references ... which ... key ... but ... fails ... )){&new_sym($fields[2] ... With the test data provided the new_sym function is never called! When using
        Message 3 of 5 , Apr 13, 2005
          --- In perl-beginner@yahoogroups.com, "hooyar66" <neville.hodder@p...>
          wrote:
          >
          > Group
          >
          > Please find my program code below and then some sample data below
          > that.
          >
          > I posted this query last week and have tried to make this easier to
          > understand. I have removed all the previous (unnecessary)
          references
          > to main::
          >
          > The problem that I am having is illustrated in two sections:
          > The first section begins: "#Print current hash key (reference)"
          which
          > works as expected, and prints the reference to a hash array.
          >
          > The next section commented "#Print first element of current hash
          key"
          > ought to print out the first element of the 'current' hash array -
          but
          > it never finds anything defined.
          > Q1. Why is this not finding an array element value?
          >
          > I have had to comment out the actual code for the "element" section
          > because if this is interpreted the following 'foreach' loop also
          fails
          > Q2. Why does my faulty code effect the 'foreach' loop?
          >
          > It is almost as if just by referring to the hash array element, the
          > entire hash and values are deleted.
          >
          > Thanks for any ideas
          > NJH
          >
          > ---------------------------------------------
          >
          > #! perl
          >
          > use strict;
          > use warnings;
          > use diagnostics;
          >
          > my ($line, %symbol_data, @current_symbol, $count);
          >
          > $count = 0;
          >
          > while ($line = <>){
          > chomp($line);
          > #Change separator
          > my @fields = split /!/, $line;
          > #Next line if current starts with A or J
          > # next if (($fields[0] =~ /(^A|^J)/)||($fields[1] eq "CHECKED"));
          > next if ($fields[0] =~ /(^A|^J)/);
          >
          > #Process line
          > my @status = &parse_line(@fields);
          >
          > #Save symbol array to hash
          > if(($status[0] eq "new")&&($status[1] eq "dims"
          )){&new_sym($fields[2]
          > )}
          >
          > #Print current hash key (reference)
          > if (exists $symbol_data{$fields[2]}){
          > print "Key)", $symbol_data{$fields[2]}, "\n";
          > }
          > #Print first element of current hash key
          > # if (exists $symbol_data{$fields[2]}->[0]){
          > # print "Element)", $symbol_data{$fields[2]}->[0], "\n";
          > # }
          >
          >
          > }
          >
          > foreach my $key (sort keys %symbol_data) {
          > $count = 0;
          > while(exists $symbol_data{$key}->[$count]){
          > print "[$count] is: ", $symbol_data{$key}->[$count], "\n";
          > $count++;
          > };
          > print "\n";
          > }
          >
          > ####################
          > sub parse_line{
          >
          > my @results = ("empty", "empty", "empty");
          >
          > #Check for new symbol
          > if($symbol_data{$_[2]}){$results[0] = "exists"}
          > else{$results[0] = "new"}
          >
          > #Numeric (dimension) data found
          > if($_[3] =~ /^\d/){$results[1] = "dims"}
          >
          > #Add new data to array
          > if(($results[0] eq "new")&&($results[1] ne "dims")){
          > $current_symbol[$count] = $_[3];
          > $count++;
          > $results[2]= "saved";
          > }
          >
          > return @results;
          > }
          >
          > ####################
          > sub new_sym{
          >
          > #Finalise new symbol
          > #Add a quantity element to @current_symbol;
          > push @current_symbol, 1;
          > @{$symbol_data{$_[0]}} = @current_symbol;
          > @current_symbol = ();
          > $count = 0;
          > }
          >
          > ####################
          >
          > =comment
          > #test data:
          > snip

          With the test data provided the new_sym function is never called!

          When using complicated data structures (hash of arrays here), look at
          'perldoc perldsc' (Data Structures Cookbook).
          Online at:
          http://www.perldoc.com/perl5.8.4/pod/perldsc.html#HASHES-OF-ARRAYS

          [Apologies for loss of formatting in code below - the Yahoo! Groups
          web interface for replies loses indents]
          Using examples in perldsc:
          1) Change the foreach loop with $count from:
          foreach my $key (sort keys %symbol_data) {
          $count = 0;
          while(exists $symbol_data{$key}->[$count]){
          print "[$count] is: ", $symbol_data{$key}->[$count], "\n";
          $count++;
          };
          print "\n";
          }to:
          foreach my $key (sort keys %symbol_data) {
          print "key = $key\n";
          foreach $count ( 0 .. $#{ $symbol_data{$key} } ) {
          print " $count is $symbol_data{$key}[$count]\n";
          }
          }

          2) In new_sym function:
          Change:
          @{$symbol_data{$_[0]}} = @current_symbol;
          to
          $symbol_data{$_[0]} = [ @current_symbol ];
        • hooyar66
          Thanks for the suggestions Damien - I ll be sure to look at that documentation. Unfortunately however if I use your code snippets, my original problem is still
          Message 4 of 5 , Apr 13, 2005
            Thanks for the suggestions Damien - I'll be sure to look at that
            documentation.

            Unfortunately however if I use your code snippets, my original problem
            is still there, i.e The first element of the hash array is still not
            printed. I still don't know the answers to my Q1 & Q2!

            I am not sure why you say the &new_sym function - without your
            suggested changes - doesn't work with the data? It works just fine for
            me providing I have the 'problem' lines of code commented, did you
            remove the quoted discussion thread from below the sample data?

            Thanks
            NJH

            --- In perl-beginner@yahoogroups.com, "Damien Carbery" <daymobrew@y...
            > wrote:
            >
            > --- In perl-beginner@yahoogroups.com, "hooyar66" <neville.hodder@p..
            .>
            > wrote:
            > >
            > > Group
            > >
            > > Please find my program code below and then some sample data below
            > > that.
            > >
            > > I posted this query last week and have tried to make this easier
            to
            > > understand. I have removed all the previous (unnecessary)
            > references
            > > to main::
            > >
            > > The problem that I am having is illustrated in two sections:
            > > The first section begins: "#Print current hash key (reference)"
            > which
            > > works as expected, and prints the reference to a hash array.
            > >
            > > The next section commented "#Print first element of current hash
            > key"
            > > ought to print out the first element of the 'current' hash array -
            > but
            > > it never finds anything defined.
            > > Q1. Why is this not finding an array element value?
            > >
            > > I have had to comment out the actual code for the "element"
            section
            > > because if this is interpreted the following 'foreach' loop also
            > fails
            > > Q2. Why does my faulty code effect the 'foreach' loop?
            > >
            > > It is almost as if just by referring to the hash array element,
            the
            > > entire hash and values are deleted.
            > >
            > > Thanks for any ideas
            > > NJH
            > >
            > > ---------------------------------------------
            > >
            > > #! perl
            > >
            > > use strict;
            > > use warnings;
            > > use diagnostics;
            > >
            > > my ($line, %symbol_data, @current_symbol, $count);
            > >
            > > $count = 0;
            > >
            > > while ($line = <>){
            > > chomp($line);
            > > #Change separator
            > > my @fields = split /!/, $line;
            > > #Next line if current starts with A or J
            > > # next if (($fields[0] =~ /(^A|^J)/)||($fields[1] eq "CHECKED"));

            > > next if ($fields[0] =~ /(^A|^J)/);
            > >
            > > #Process line
            > > my @status = &parse_line(@fields);
            > >
            > > #Save symbol array to hash
            > > if(($status[0] eq "new")&&($status[1] eq "dims"
            > )){&new_sym($fields[2]
            > > )}
            > >
            > > #Print current hash key (reference)
            > > if (exists $symbol_data{$fields[2]}){
            > > print "Key)", $symbol_data{$fields[2]}, "\n";
            > > }
            > > #Print first element of current hash key
            > > # if (exists $symbol_data{$fields[2]}->[0]){
            > > # print "Element)", $symbol_data{$fields[2]}->[0], "\n";
            > > # }
            > >
            > >
            > > }
            > >
            > > foreach my $key (sort keys %symbol_data) {
            > > $count = 0;
            > > while(exists $symbol_data{$key}->[$count]){
            > > print "[$count] is: ", $symbol_data{$key}->[$count], "\n";
            > > $count++;
            > > };
            > > print "\n";
            > > }
            > >
            > > ####################
            > > sub parse_line{
            > >
            > > my @results = ("empty", "empty", "empty");
            > >
            > > #Check for new symbol
            > > if($symbol_data{$_[2]}){$results[0] = "exists"}
            > > else{$results[0] = "new"}
            > >
            > > #Numeric (dimension) data found
            > > if($_[3] =~ /^\d/){$results[1] = "dims"}
            > >
            > > #Add new data to array
            > > if(($results[0] eq "new")&&($results[1] ne "dims")){
            > > $current_symbol[$count] = $_[3];
            > > $count++;
            > > $results[2]= "saved";
            > > }
            > >
            > > return @results;
            > > }
            > >
            > > ####################
            > > sub new_sym{
            > >
            > > #Finalise new symbol
            > > #Add a quantity element to @current_symbol;
            > > push @current_symbol, 1;
            > > @{$symbol_data{$_[0]}} = @current_symbol;
            > > @current_symbol = ();
            > > $count = 0;
            > > }
            > >
            > > ####################
            > >
            > > =comment
            > > #test data:
            > > snip
            >
            > With the test data provided the new_sym function is never called!
            >
            > When using complicated data structures (hash of arrays here), look
            at
            > 'perldoc perldsc' (Data Structures Cookbook).
            > Online at:
            > http://www.perldoc.com/perl5.8.4/pod/perldsc.html#HASHES-OF-ARRAYS
            >
            > [Apologies for loss of formatting in code below - the Yahoo! Groups
            > web interface for replies loses indents]
            > Using examples in perldsc:
            > 1) Change the foreach loop with $count from:
            > foreach my $key (sort keys %symbol_data) {
            > $count = 0;
            > while(exists $symbol_data{$key}->[$count]){
            > print "[$count] is: ", $symbol_data{$key}->[$count], "\n";
            > $count++;
            > };
            > print "\n";
            > }to:
            > foreach my $key (sort keys %symbol_data) {
            > print "key = $key\n";
            > foreach $count ( 0 .. $#{ $symbol_data{$key} } ) {
            > print " $count is $symbol_data{$key}[$count]\n";
            > }
            > }
            >
            > 2) In new_sym function:
            > Change:
            > @{$symbol_data{$_[0]}} = @current_symbol;
            > to
            > $symbol_data{$_[0]} = [ @current_symbol ];
          Your message has been successfully submitted and would be delivered to recipients shortly.