Tuesday, November 20, 2001, 7:11:34 PM, Wagner-David wrote:

WD>         Here is a start:
WD> Script starts on next line:
WD> #!perl -w

WD> printf "%-20s - %-s\n", "SortField", "Url";
WD> foreach my $MyData (sort {$a->[2] cmp $b->[2]} map{ 
[$_,/="([^"]+).+\>([^<]+)\<\/a/i ] } <DATA> ) {
WD>    printf "%-20s - %-s\n", $MyData->[2], $MyData->[1];
WD>  }

for those who won't have any clues whatsoever about that hunk of line
noise, i've expanded it out a bit and added some commentary.

  1: my @links;
  2: foreach my $line (<DATA>) {
  3:   my ($link, $desc) = $line =~ /="([^"]+).+\>([^<]+)\<\/a/i;
  4:   push @links, [ $link, $desc ];
  5: }
  6:
  7: my @sorted_links = sort { $a->[2] cmp $b->[2] } @links;
  8:
  9: foreach my $line (@sorted_links) {
 10:    printf "%-20s - %-s\n", $line->[2], $line->[1];
 11: }
 
the overall aim is to read each line from the data file (what's under
__DATA__ [1]), find the interesting parts of each line, sort them, and
then print them.

using angle brackets around a file descriptor ("DATA" in this case)
will read a line in scalar context and the whole file in array
context. so our foreach line will read a line from the file, store it
in the "$line" variable and then execute the code within the loop.

that scary looking line 3 is, in fact, scary. it will pattern match on
$line, returning the right bits.

  /
   ="      # match an = then a " (like in href="
   ([^"]+) # then go find everything that's not a
           # quote and remember it
   .+      # find something, one or more time
   \>      # then a literal >
   ([^<]+) # everything that's not a < and remember it
   \<\/a   # then the string "</a"
  /i       # and make it case insensitive too

see, i said it was scary... [5] might help...

line 4:
  4:   push @links, [ $link, $desc ];

this will create an anonymous array [2] with two items in it and store
a reference to that anonymous array in the @links array... (read [2]
and it might make sense...)

  7: my @sorted_links = sort { $a->[1] cmp $b->[1] } @links;

we need to sort the data, so we use the "sort" function [3]. this
function allows us to specify how we'd like the sorting to happen, and
in this case we want to sort on the anchor text. if you remember from
line 4 it's stored in the second element of our anonymous array.
because the first element is indexed at 0, the second is at position
1.

that'll give us all of the links sorted properly, so now we need to
print them out:

  9: foreach my $line (@sorted_links) {
 10:    printf "%-20s - %-s\n", $line->[1], $line->[0];

printf is like doing a print and an sprintf [4]. that funny string
with the %'s in it is a template which says that we'd like the data
formatted please and how we'd like it formatted. in this case, we'd
like the first field left-justified within 20 spaces and the second
field just left-justified. [4] has good info about it.


hope that helps someone!


-- 
Best regards,
 Daniel


[1] http://www.perldoc.com/perl5.6.1/lib/SelfLoader.html#The-__DATA__-token
[2] http://www.perldoc.com/perl5.6.1/pod/perlreftut.html
[3] http://www.perldoc.com/perl5.6.1/pod/func/sort.html
[4] http://www.perldoc.com/perl5.6.1/pod/func/sprintf.html
[5] http://www.perldoc.com/perl5.6.1/pod/perlretut.html


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to