AT&F1&K3&W OK Jim Correia said in Re: [MacPerl-Scribes] "Basic Perl Filters in BBEdit" Colum at Apr/19/2000, Wed 13:30:03. > Here is quickie example that reverses the selected lines. If it looks > like C it is because I write C all day long and it influences my perl > (of which I don't do or know too much): > > #!perl -w > use strict; > > while ( <> ) > { > my $s = $_; > > chomp($s); > > my $ct = length($s); > > for(my $i=$ct-1; $i>=0; $i--) > { > print substr($s, $i, 1); > } > > print "\n"; > } Yup, looks like a C programmer wrote it. ;-) Yes, I'm still here. Snail-mail hasn't arrived with my memory yet, and the local parts place (where I can get a 28.8K modem for $6) just now opened for the day. Jim's script is a good case study on using #! switches. Perl also does advanced string handling far better than C (which as I recall doesn't actually *do* string handling, but I never got anywhere close to understanding C), so a simple: #!perl -pl $_ = reverse does the same thing. This kinda stuff happens on BBEdit-Talk a lot, seeing as Perl is a second language to a many programmers. Jim's version also works, BTW, with much re-invention of the wheel. ;-) I'm not, conversely, able to hold my own on *his* ground, so this says nothing against Jim. I'd be at a complete loss in C (C being one of the fifteen "Seven programming languages I failed to become proficient in before learning Perl"), so that Jim's script runs, and works as expected is either a tribute to Perl or Jim or both. Now perhaps, since I'm online at the moment, I can explain how to get from Jim's example to mine: First, note that the entire script is encased in a while(). This calls for either -p or -n on the hash-bang to remove that construction. Second, note that the while() ends with a print(). This makes it -p without a doubt. Third, note that the script chomp()s input and appends "\n" to its output. Assigning $_ to $s is an uncessary step. It's more idiomatically perlish to just "chomp;", which performs chomp($_) by default, thus dropping $s from the namespace. This calls for -l, thus removing both chomp() and print(). Since the purpose of the for() loop is duplicated by the built-in function reverse(), and reverse applies to $_ by default, the entire for loop becomes "$_ = reverse", and all the associated variables are dropped from the namespace. The idiomatic version is **trivially** faster and less memory intensive. In practice, both versions are too fast to time. +++ NO CARRIER ==== Want to unsubscribe from this list? ==== Send mail with body "unsubscribe" to macperl-scribes-request@macperl.org