Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: how to shor...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 7 Topic 11035 of 11401
Post > Topic >>

Re: how to shorten this code ? x occur how many times in variable

by rich.japh@[EMAIL PROTECTED] (Richard Lee) May 7, 2008 at 02:07 PM

Rob Dixon wrote:
> Richard Lee wrote:
>   
>> John W. Krahn wrote:
>>
>>     
>>> Richard Lee wrote:
>>>
>>>       
>>>> Can you please tell me how to shorten this?
>>>>
>>>> my @[EMAIL PROTECTED]
 = split(//);
>>>> my @[EMAIL PROTECTED]
 = grep { $_ eq ':' } @[EMAIL PROTECTED]
 ;
>>>>
>>>> I was trying to see how many : occur in variable but didn't know how 
>>>> to do it fast so i did it like above...
>>>>
>>>> I would like to see as many way different ways to get this done if 
>>>> possible
>>>>
>>>>
>>>> $var1 = root:x:123:/root:
>>>>
>>>> trying to see how many times : occurs in $var1..... and I could only 
>>>> do it above way....
>>>>         
>>> my $count = $var1 =~ tr/://;
>>>       
>> Talking about reinventing the wheel!!  thank you and I shall try this..

>> as I have nver tried tr before.......... thank you!!
>>     
>
> tr/// is in the long tradition of Unix misnomers. It is comparable to
s///g, but
> tr(anslates) characters in the first list to those in the second list,
so
>
>   $str =~ tr/;:/../;
>
> would replace all colons and semicolons with full stops. It returns the
number
> of characters it has translated. However, if there is no character in
the
> corresponding position in the second list the character remains
unchanged but
> the count is still maintained, so
>
>   $str =~ tr/://;
>
> returns the number of colons found but leaves them untouched. Similarly
>
>   $str =? tr/\t /_/;
>
> translates all tabs to underscores, leaves spaces untouched, and returns
the
> total number of either tab or spaces found in the object string.
>
> It is Perl idiom, and you should learn it.
>
> (If you want to be even more obscure then y/// is a synonym for tr///)
>
> HTH,
>
> Rob
>   

this is great!!

use warnings;
use strict;

my $str = 'ab:cd:ef:g:hi::now;';
print $str =~ tr/:// . "\n";
print $str =~ tr/:b/_X/ . "\n";

print "$str\n";


.././././././testthis.pl
6
7
aX_cd_ef_g_hi__now;

BTW, what is =?    ? or is that a typo?

$str =? tr/\t /_/;
 




 7 Posts in Topic:
how to shorten this code ? x occur how many times in variable
rich.japh@[EMAIL PROTECTE  2008-05-07 01:43:29 
Re: how to shorten this code ? x occur how many times in variabl
krahnj@[EMAIL PROTECTED]   2008-05-06 23:28:58 
Re: how to shorten this code ? x occur how many times in variabl
rich.japh@[EMAIL PROTECTE  2008-05-07 03:12:03 
Re: how to shorten this code ? x occur how many times in variabl
rob.dixon@[EMAIL PROTECTE  2008-05-07 16:11:07 
Re: how to shorten this code ? x occur how many times in variabl
rich.japh@[EMAIL PROTECTE  2008-05-07 14:07:04 
Re: how to shorten this code ? x occur how many times in variabl
krahnj@[EMAIL PROTECTED]   2008-05-07 18:15:25 
Re: how to shorten this code ? x occur how many times in variabl
rich.japh@[EMAIL PROTECTE  2008-05-08 00:15:37 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Jul 6 7:00:44 CDT 2008.