Thursday, July 30, 2009

How do I display an array which contains structures using a foreach statement in Perl?

I have the following code:


while (%26lt;code I can't post%26gt;) {


$Request-%26gt;{project} = $resultSetObj-%26gt;GetColumnValue(1);


$Request-%26gt;{id} = $resultSetObj-%26gt;GetColumnValue(2);


$Request-%26gt;{title} = $resultSetObj-%26gt;GetColumnValue(3);


print("$Request-%26gt;{project}, $Request-%26gt;{id}, $Request-%26gt;{title}\n"); # This prints the data I need prior to the array push.





push( @Requests, $Request );


}


print( scalar( @Requests . "\n" ) ); # Prints number of Array elements (144 in my case)


print("Printing array...\n");


foreach $Request (@Requests) {


printf ($Request-%26gt;{project}, $Request-%26gt;{id}, $Request-%26gt;{title}."\n");


}





The foreach statement prints the last element in the array only. How can I appropriately step through each element of the array and display the contents?

How do I display an array which contains structures using a foreach statement in Perl?
#!perl


my @Requests = ({project=%26gt;'x', id=%26gt;'one',title=%26gt;'a title'}, {project=%26gt;'x', id=%26gt;'two', title=%26gt;'second title'}, {project=%26gt;'x', id=%26gt;'three', title=%26gt;'third title'});











print( scalar( @Requests . "\n" ) ); # Prints number of Array elements (144 in my case)


print "Printing array...\n";


foreach $Request (@Requests) {


print $Request-%26gt;{project}, $Request-%26gt;{id}, $Request-%26gt;{title},"\n";


}





3


Printing array...


xonea title


xtwosecond title


xthreethird title


No comments:

Post a Comment