--- David Gilden <[EMAIL PROTECTED]> wrote:
> As an exercise here I want to play with this for a few minutes,
> and have a few basic questions
> 
> #!/usr/bin/perl
> 
> # First question  do I use 
> # ( .... )  OR { ... }
> @lines = qq{dave john mike drew};

It's not that simple, and it's simpler.

First, qq{foo} is exatly the same as qq(foo), and even qq/foo/.
The trick is that if you use bracketing characters like () {} [] <>
then one opens, the other closes. If you use non-bracketing characters
like / | ! ' (or whatever) then the first opens, the next closes. See
perdoc perlop (I think).

Next, qq{foo} is the same as "foo". qq is the interpolating operator,
intended to let you embed various types of quotes, among other things,
like this:

  print qq{ I said "She said 'Hi' to me" to George.};

qq{foo} is "foo", just as q{foo} is 'foo'.  Double-q, double-quote,
single-q, single-quote.

so what you've done with
> @lines = qq{dave john mike drew};
is exactly the same as
  @lines = "dave john mike drew";

In the other hand, qw{} is often pronounced "Quote-Words", and it
returns a list of the whitespace-delimited strings it encloses. 
Thus,
  qw / a b c /
is equivelent to 
  ('a', 'b', 'c')

(I don't have Perl installed here at home yet, so somebody please
correct any of this that I'm fouling up off the top of my head! lol!)

> # First error, should be qw( ... )
> # qq is garbage, right?

Well, depends on what you want, lol....
But in this use, probably.

> # is the  ','  correct in my($a,$b) can you use a space my($a $b @c)

The comma is correct, an no, I don't think a space works in my().
 
> my (%sortKeys,$i);

That declares %sortKeys and $i.

> foreach ( @lines ) {

That will iterate over each element of @lines, aliasing each to $_

That's fine, but a more explicit syntax would be to declare a
loop-local variable to use as the alias.

  foreach my $line (@lines) { # assines each elem of @lines to $line

>     print"$_$i\n";

Assuming you didn't print the code that put a value in $i, ok.
If this is all the code you're using, then $i hasn't been loaded, but
you *did* make $_ an alias to each element of @lines, so it should
print whatever's in @lines.
     
>     my $sortKey = $i++; # do something to create the sort key,
>                       # using %idToName to map the ID to the name

Just remember that this assigns the value of $i *before* incrementing
$i. That may be exactly what you wanted -- just being explicit. =o)

>     $sortKeys{$_} = $sortKey;

Which assigns the value of $sortKey to the element of %sortKeys at the
location indexed by $_, which has whichever element of @lines it got
this iteration. 

>    }
> 
> # Then create a sub to pass to sort:
> 
> print "@lines $i\n";
> 
> # Could you elaborate on this
> # $a $b are not passed any values -- don't understand 
> sub bySortKey {
>     $sortKey{$a} cmp $sortKey{$b}
> }
>  print &bySortKey; 

Ok -- let's look at some code.

 my %h = (1..100); # $h{1}==2, $h{3}==4, etc.... A quick shuffle
 my @ary = keys %h;

@ary now has a hash-order list of the odd numbers less than 100.
All that's just to have something to sort.

I could say 
  my @srt = sort @ary;

and sort would walk through @ary, using the default comparison, putting
them in order. (Question: isn't the default a string compare? That
would but 10-19 before 2, and 20-29 before 3.....)

So, maybe, since I seem a little unsure, I want to be certain to put
them in numeric sort order. One way to do that is to build a comparicon
function right inside the sort statement, like this:

  my @srt = sort { $a <=> $b } @ary;

In a case like this, the elements being compared are given to the
routine I built in the middle of the sort as $a and $b. That's just a
standard Perl-ism. Sorts provide $a and $b as the default variables.
Had I wanted them backwards, I could have said:

  my @srt = sort { $b <=> $a } @ary;

And it would reverse-sort.
 
This is the way I usually do this sort of thing, but you can also write
a named subroutine to handle it, in which case the syntax would be 

  my @srt = sort funcName @ary;

sort would then call your subroutine named funcName to evaluate the
comparison of elements. It would still be able to use $a and $b, which
are package globals. So funcName (which might actually be bySortKey)
might look like you wrote it above:

> sub bySortKey {
>     $sortKey{$a} cmp $sortKey{$b}
> }

but to print the sorted values, rather than

>  print &bySortKey; 

you might say

   print sort bySortKey @ary;

Of course, you'd need to populate %sortKey first, which I didn't. But
if I wanted to use %h, I could:

> sub funcName {
>     $h{$a} cmp $h{$b}
> }

For my example values that would be silly, but that happens a lot in
examples, I'm told. lol....

> exit(0); # If script executes successfully do you exit(1) OR exit(0);

usually, if everything it fine, you don't need either. just say:

  exit;

Passing an argument explicitly says to use that for your return code to
the operating system.

  exit; # same as exit(0)

  exit($someCode); # returns $someCode to system

And I really do seem dead tired here.
Somebody double-check all this?
I'm probably babbling.....

Still, hope it helps. :o)


__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply via email to