Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 09-30-2002, 10:29 PM   #21
Titanium Man
Guest
 
Posts: n/a
Ah, thanks again. I'm just curious about that trailing d though, in

$app=~tr/!$&\"'*()]}{[|;<>?~`\ //d;

What does that do? Is this a regexp thing that I don't get yet?
  Reply With Quote
Old 10-01-2002, 09:11 AM   #22
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
The "d" modifier to the "tr" operator means "delete any characters in the first chunk (ie between the first pair of forward slashes in the examples we've been using up to now) that don't have replacements listed in the second chunk. If you just leave the second chunk empty, as in

$app=~tr/!$&\"'*()]}{[|;<>?~`\ //;

the default behaviour of "tr" is to replicate the search list in the replacement list (ie to act as if the second chunk contained everything in the first chunk). This has the useful side-effect of giving a quick way to count characters.

#!/usr/bin/perl -w
use strict;
my $string = "A honking great piece of text with
some newlines in it, and a
plethora of a's";
my $count = ($string=~tr/a//);
print $count; # prints 5 (there are five a's in the string)
my $newlines = ($string=~tr/\n//);
print $newlines; # prints 2 (there are two newlines in the string)
### BUT NOW###
$string=~tr/\n//d; # adding in the delete modifier
print $string; # Strips the newlines to produce the single line version of $string

A honking great piece of text with some newlines in it, and a plethora of a's

Cheers,
Paul
pmccann is offline   Reply With Quote
Old 10-01-2002, 10:34 AM   #23
Titanium Man
Guest
 
Posts: n/a
The light of understanding is creeping in. Thanks!
  Reply With Quote
Old 10-11-2002, 01:20 AM   #24
thatch
All Star
 
Join Date: Jan 2002
Posts: 534
Thumbs up Thank you Paul

After carefully reading through everything and making the necessary adjustments as described, it works flawlessly. Awesome, you're a wizard!

thatch is offline   Reply With Quote
Old 10-11-2002, 09:50 AM   #25
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
Thanks for the feedback thatch, and sorry that the road through this thread is both long and a little bumpy. Still, I guess it illustrates rather nicely the sort of nonsense that it takes to get a script in reasonable working order; warts and all.

For what it's worth, after a little experimentation I've found the "empty" version to be easily the most useful: that is, where spaces and quotes and ampersands and the like are replaced by nothingness. So

/Applications/Utilities/Disk Copy.app/ is called by "diskcopy"

and so forth. Underscores are too hard to type!

Cheers,
Paul
pmccann is offline   Reply With Quote
Old 10-11-2002, 04:24 PM   #26
thatch
All Star
 
Join Date: Jan 2002
Posts: 534
Arrow No worries...

The bumpy road provided an excellent learning experience for everyone. And it wasn't really all that rough anyhow. I enjoyed the ride. Tomorrow's journey may be the smoother for it all.

And, I agree that keeping the typing simple is the best course. In its refined state, this adventure is one to be proud of for sure. I hope many others partake in the thrill.

You're very inspiring, Paul. I hope you never stop this behavior. It becomes you.

Regards,

thatch

thatch is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:36 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.