how to shorten this code ? x occur how many times in variable
  Home FAQ Contact Sign in
perl.beginners only
 
Advanced search
POPULAR GROUPS

more...

perl.beginners Profile…
 Up
how to shorten this code ? x occur how many times in variable         


Author: Richard Lee
Date: May 6, 2008 22:43

Can you please tell me how to shorten this?

my @an = split(//);
my @num = grep { $_ eq ':' } @an ;

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

thank you!!

$var1 = root:x:123:/root:

trying to see how many times : occurs in $var1..... and I could only do
it above way....
6 Comments
Re: how to shorten this code ? x occur how many times in variable         


Author: John W. Krahn
Date: May 6, 2008 23:28

Richard Lee wrote:
> Can you please tell me how to shorten this?
>
> my @an = split(//);
> my @num = grep { $_ eq ':' } @an ;
>
> 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
>
> thank you!!
>
>
> $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/://;
Show full article (0.70Kb)
no comments
Re: how to shorten this code ? x occur how many times in variable         


Author: Richard Lee
Date: May 7, 2008 00:12

John W. Krahn wrote:
> Richard Lee wrote:
>> Can you please tell me how to shorten this?
>>
>> my @an = split(//);
>> my @num = grep { $_ eq ':' } @an ;
>>
>> 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
>>
>> thank you!!
>>
>>
>> $var1 = root:x:123:/root:
>>
>> trying to see how many times : occurs in $var1..... and I could only
>> do it above way.... ...
Show full article (0.70Kb)
no comments
Re: how to shorten this code ? x occur how many times in variable         


Author: Rob Dixon
Date: May 7, 2008 08:11

Richard Lee wrote:
>
> John W. Krahn wrote:
>
>> Richard Lee wrote:
>>
>>> Can you please tell me how to shorten this?
>>>
>>> my @an = split(//);
>>> my @num = grep { $_ eq ':' } @an ;
>>>
>>> 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:
>>> ...
Show full article (1.55Kb)
no comments
Re: how to shorten this code ? x occur how many times in variable         


Author: Richard Lee
Date: May 7, 2008 11:07

Rob Dixon wrote:
> Richard Lee wrote:
>
>> John W. Krahn wrote:
>>
>>
>>> Richard Lee wrote:
>>>
>>>
>>>> Can you please tell me how to shorten this?
>>>>
>>>> my @an = split(//);
>>>> my @num = grep { $_ eq ':' } @an ;
>>>>
>>>> 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
>>>> ...
Show full article (1.98Kb)
no comments
Re: how to shorten this code ? x occur how many times in variable         


Author: John W. Krahn
Date: May 7, 2008 18:15

Richard Lee wrote:
> Rob Dixon wrote:
>>
>> returns the number of colons found but leaves them untouched. Similarly
>>
>> $str =? tr/\t /_/;
>
> 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 ...
Show full article (0.66Kb)
no comments
Re: how to shorten this code ? x occur how many times in variable         


Author: Richard Lee
Date: May 7, 2008 21:15

John W. Krahn wrote:
> Richard Lee wrote:
>> Rob Dixon wrote:
>>>
>>> returns the number of colons found but leaves them untouched. Similarly
>>>
>>> $str =? tr/\t /_/;
>>
>> 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";
>>
>> ...
Show full article (0.69Kb)
no comments