CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
XPath, XMLParser, and an anonymous array troubles

 



set5think
Novice

Oct 12, 2008, 11:15 PM

Post #1 of 5 (904 views)
XPath, XMLParser, and an anonymous array troubles Can't Post

Okay, I am at a total loss, and am considering breaking something, so please someone help me.

I am trying to use an anonymous array of arrays, send that reference off to a subroutine which then dereferences the variable, and then I'm trying to use two separate counters to make this xml parse dynamic.

Let's start with my arrays:


Code
my @offensive_stats = qw(offensive-plays-yards 
offensive-plays-number
offensive-plays-average-yards-per);
my @passing_stats = qw(passes-yards-net
passes-yards-gross
times-thrown-total
passes-yards-lost
passes-attempts
passes-completions
passes-interceptions
passes-average-yards-per);


There are a couple more arrays with several entries, but for the purpose of the example, two should be enough.

Then here's my anonymous array of arrays:

Code
my $offensive = 
[
[@offensive_stats],
[@passing_stats],
[@rushing_stats],
[@downprogress_stats],
];

Here's this other anonymous array that contains some strings to build my path in the xml file(this will make more sense when we get to the subroutine):

Code
my $path_ref= ['offensive','passing','rushing','down-progress'];

Here's my call to my subroutine so you see I'm passing it through properly:

Code
&get_team_stats(\@$offensive, \@$path_ref);

And finally, here is the subroutine:

Code
sub get_team_stats { 
my $ref1 = $_[0];
my $ref2 = $_[1];
my $counter1 = 0;
while ($while) {
$alt_counter = 0;
while ($while) {

my $attribute = @{$ref1}->[$counter1][$alt_counter];
#print "$base_path/team[1]/team-stats/team-stats-american-football/stats-american-football-".@{$ref2}->[$counter1].'/@'.$attribute."\n";
$team_info_hash{$attribute} = &get_data("$base_path/team[1]/team-stats/team-stats-american-football/stats-american-football-".@{$ref2}->[$counter1].'/@'.$attribute);
#print "this is a ref $attribute $team_info_hash{$attribute} ocounter: $counter1 altcounter: $alt_counter\n";
last if (&get_data("$base_path/team[1]/team-stats/team-stats-american-football/stats-american-football-".@{$ref2}->[$counter1].'/@'.$attribute) eq '');
$alt_counter++;
}
last if ($counter1 == 4);
$counter1++;
}
}

the reason my "last if" condition is checking to end it if the counter = 4 is just for debugging purposes.

Anyway, the problem lies in that it's not working on the condition that if the &get_data subroutine doesn't find anything at the path it's at, then stop the loop, and perform the increment and move on to the next path if there is one. The results are that I have an invalid query. I'll attach my code in here as well as a test xml file. You won't need to type the name of the file as a command line argument, since I've made the script dependent on this particular xml file for your convenience. Thank you in advance to you brave souls.


(This post was edited by set5think on Oct 13, 2008, 2:00 AM)


set5think
Novice

Oct 13, 2008, 2:03 AM

Post #2 of 5 (899 views)
Re: [set5think] XPath, XMLParser, and an anonymous array troubles [In reply to] Can't Post

Alright, I don't know why I didn't realize it for the past couple days, but I was passing a null value to a subroutine, that's why it was calling invalid. I should've been checking on the actual array element to see if it was null or not, and if it wasn't, then call the get_data subroutine. I am leaving this posting up so that everyone can see what a noob I am.


shawnhcorey
Enthusiast


Oct 13, 2008, 2:56 AM

Post #3 of 5 (896 views)
Re: [set5think] XPath, XMLParser, and an anonymous array troubles [In reply to] Can't Post


In Reply To

Code
my $offensive = 
[
[@offensive_stats],
[@passing_stats],
[@rushing_stats],
[@downprogress_stats],
];


my $path_ref= ['offensive','passing','rushing','down-progress'];

&get_team_stats(\@$offensive, \@$path_ref);



Just at a glance, change the last line to:

Code
get_team_stats($offensive, $path_ref)


Since they both already are references, there is no need to dereference them and re-reference them. Also, don't use & for sub calls.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


set5think
Novice

Oct 13, 2008, 10:43 PM

Post #4 of 5 (858 views)
Re: [shawnhcorey] XPath, XMLParser, and an anonymous array troubles [In reply to] Can't Post

Hey; thanks for the reply and suggestion. Why would you recommend not using the '&' for the subroutine calls? I've read it's better to use it than not, but I've really known the real difference; I've heard that you should use the '&' if you're expecting a value returned. Look forward to your explanation.


shawnhcorey
Enthusiast


Oct 14, 2008, 4:39 AM

Post #5 of 5 (846 views)
Re: [set5think] XPath, XMLParser, and an anonymous array troubles [In reply to] Can't Post


In Reply To
Hey; thanks for the reply and suggestion. Why would you recommend not using the '&' for the subroutine calls? I've read it's better to use it than not, but I've really known the real difference; I've heard that you should use the '&' if you're expecting a value returned. Look forward to your explanation.


The use of & hasn't been recommended since Perl 4. If you use it, you are not protected from spelling mistakes. In the code below, the lack of & causes a compile error. With &, it's a runtime error. The difference may not be significant is such a small program but in a large one it's better to know of the error as soon as possible.


Code
#!/usr/bin/perl 

use strict;
use warnings;

sub foo {
print "in foo\n";
}

foo;
fooo;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives