I've got some code that a friend of mine helped create:
1 use LWP::Simple;
2 use HTML::TreeBuilder;
3 use Data::Dumper;
4
5 my $tree = url_to_tree( 'http://www.registrar.ucla.edu/schedule/schedulehome.aspx' );
6
7 my @selects = $tree->look_down( _tag => 'select' );
8 my @quarters = map { $_->attr( 'value' ) } $selects[0]->look_down( _tag => 'option' );
9 my @courses = map { my $s = $_->attr( 'value' ); $s =~ s/&/%26/g; $s =~ s/ /+/g; $s } $selects[1]->look_down( _tag => 'option' );
10
11 my $n = 0;
12
13 my %hash;
14
15 for my $quarter ( @quarters )
16 {
17 for my $course ( @courses )
18 {
19 my $tree_b = url_to_tree( "http://www.registrar.ucla.edu/schedule/crsredir.aspx?termsel=$quarter&subareasel=$course" );
20
21 my @options = map { my $s = $_->attr( 'value' ); $s =~ s/&/%26/g; $s =~ s/ /+/g; $s } $tree_b->look_down( _tag => 'option' );
22
23 for my $option ( @options )
24 {
25
26
27 print "trying: http://www.registrar.ucla.edu/schedule/detselect.aspx?termsel=$quarter&subareasel=$course&idxcrs=$option\n";
28
29 my $content = get( "http://www.registrar.ucla.edu/schedule/detselect.aspx?termsel=$quarter&subareasel=$course&idxcrs=$option" );
30
31 next if $content =~ m/No classes are scheduled for this subject area this quarter/;
32
33 $hash{"$course-$option"} = 1;
34 #my $tree_c = url_to_tree( "http://www.registrar.ucla.edu/schedule/detselect.aspx?termsel=$quarter&subareasel=$course&idxcrs=$option" );
35
36 #my $table = ($tree_c->look_down( _tag => 'table' ))[2]->as_HTML;
37
38 #print "$table\n\n\n\n\n\n\n\n\n\n";
39
40 $n++;
41 }
42 }
43 }
44
45 my $hash_count = keys %hash;
46 print "$n, $hash_count\n";
47
48 sub url_to_tree
49 {
50 my $url = shift;
51
52 my $content = get( $url );
53
54 my $tree = HTML::TreeBuilder->new_from_content( $content );
55
56 return $tree;
57 }
I'm having trouble understanding what lines 33
and 45
are doing. I think for the most part I get what everything else is doing, namely that @selects
puts all the things contained in the two select tags in the master .aspx file on the website under consideration--I think the size of @selects
is 2. I also get that from this point the 0-th slot of @selects
is passed into @quarters
, and similarly the position-1 slot is passed into @courses. Every unique match is enumerated and so n
is the total number of courses offered throughout the year. Now, what I don't get is what $hash_count is enumerating. I suspect it is the number of unique courses offered, so where as n
is an animal something akin to (in pseudocode)
sizeof( ['math1 FALL 2014' , 'math1 SPRING 2014'] ) = 2
I suspect hash_count
is an animal like
sizeof( ['math1 FALL 2014' , 'math1 SPRING 2014'] ) = 1
Right?