> -----Original Message----- > From: John W. Brooking IV [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, December 26, 2001 10:16 AM > To: [EMAIL PROTECTED] > Subject: Syntax problems with complex data structure > > > Readers, > > I hope you all have had or are having happy holidays of > your tradition. > > I'm trying to build up a complex data structure from a > file, and cannot > figure out the right syntax for accessing it afterwards. O'Reilly's > "Programming Perl" has been very helpful in showing me how to > build it, but > I'm having trouble translating from its examples to mine in terms of > accessing it afterwards, especially after I get it back from > the function > that builds it. I suspect my confusion may have something to do with > references. > > My data structure is a hash with two key/value pairs. The > first value is > another hash, and the second is an array of hashes. The > following function > is an abbreviated sample of the code that builds it. (In the > real code, I > read values from a CSV file, but I've left that out of this > example program > for the sake of clarity.) I have put some print statements inside the > function to show that the assignments seem to be working, and > the syntax > works fine there. The function returns the plain hash (not a > reference, but > maybe it should be?), and the $tableDef{table}{name} syntax > is working fine > outside the function, but the $tableDef{colDef}[0]{name} > syntax only works > inside the function. > > So when I run this code as is, I get (line 48 is the last line) > > syntax error at test3.pl line 48, near "print" > Execution of test3.pl aborted due to compilation errors.
The line above that one is missing the semicolon. Your example works fine for me if I add the semicolon. > > If I comment that line out, I get: > > $returnData{colDefs}[0]{name} = "Category" > $returnData{colDefs}[1]{name} = "Label" > $returnData{colDefs}[2]{name} = "URL" > $returnData{colDefs}[3]{name} = "Description" > > Table Links: Links to other sites > Columns (explicitly) > > So why does the same syntax that works inside the function not work > outside it? Should I be returning a reference to the hash > instead of the > hash itself? (Which would definately change the syntax > anyway.) In either > case, what syntax will work outside the function? The missing semicolon appears to be the entire problem. To return a hashref from the sub, you would change return %returnData; to return \%returnData; Then in the main program, call it like this: my $tableDef = readfile(); # $tableDef is ref to hash and access it like this: print " $tableDef->{colDefs}[0]{name}\n"; # note "->" deref operator > I would > like to know how > to loop through the array using a foreach loop setting the > loop variable to > be each hash in the array, as well using a for loop with the > numeric array > index as the loop variable or just accessing the array > elements by literal > number. # iterate through list of hashrefs for my $href (@{$tableDef->{colDefs}}) { print "$href->{name}\n"; } # use an index for my $i (0 .. $#{$tableDef->{colDefs}}) { print "$tableDef->{colDefs}[$i]{name}\n"; } > > In case it makes a difference, I'm using ActiveState Perl > 5.6.1, binary > build 626. > > Thanks in advance for any advice. > > - John Brooking > Portland, Maine > > ------ 8< --- begin code sample --- >8 ----- > > sub readfile{ > my %returnData; > > # Create the table hash element > $returnData{table} = { name => "Links" > , desc => "Links to other sites" > }; > > # Now create the array of column definitions > my @fields; > push @fields, { name => "Category" > , desc => "Which category of links?" > , type => "reference" > , presentation => "" > }; > push @fields, { name => "Label" > , desc => "What should this link be called?" > , type => "character" > , presentation => "length:25" > }; > push @fields, { name => "URL" > , desc => "Web address" > , type => "character" > , presentation => "length:50" > }; > push @fields, { name => "Description" > , desc => "A short description of this site" > , type => "character" > , presentation => "length:2:30" > }; > $returnData{colDefs} = [@fields]; > > # Test the values > print '$returnData{colDefs}[0]{name} = "' . > "$returnData{colDefs}[0]{name}\"\n"; > print '$returnData{colDefs}[1]{name} = "' . > "$returnData{colDefs}[1]{name}\"\n"; > print '$returnData{colDefs}[2]{name} = "' . > "$returnData{colDefs}[2]{name}\"\n"; > print '$returnData{colDefs}[3]{name} = "' . > "$returnData{colDefs}[3]{name}\"\n"; > print "\n"; > > # Return the big hash > return %returnData; > > } # sub readfile > > my %tableDef = readfile(); > print "Table $tableDef{table}{name}: $tableDef{table}{desc}\n"; > print "Columns (explicitly)\n" This line is missing a semicolon. > print " $tableDef{colDefs}[0]{name}\n"; > > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]