Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

index using a range

by romano (Initiate)
on Jun 13, 2013 at 22:47 UTC ( #1038852=perlquestion: print w/ replies, xml ) Need Help??
romano has asked for the wisdom of the Perl Monks concerning the following question:

I have a string composed of numbers. Is there a way to find the index for the first non-zero character 1-9? Can I do this directly? I know I can use 'pos' or substitute for non-zero values and then do index but is there a way to do that directly?
$Str1 = "000001"; $Index = index($Str1, [1-9]); print "$Index\n";
This does not work.

grazie,

romano

Comment on index using a range
Download Code
Re: index using a range
by vsespb (Hermit) on Jun 13, 2013 at 22:57 UTC
    $Str1 = "000001"; print length($1) if $Str1 =~ /^(.*)[0-9]$/;
    UPD:
    Probably better /^(.*)[0-9]/; (without '$' in the end of regexp)
Re: index using a range
by rnewsham (Hermit) on Jun 13, 2013 at 23:06 UTC

    A slight variation on the solution proposed by vsespb, I think this is a little closer to what you want.

    my $Str1 = "000001"; my $Index = length($1) if $Str1 =~ /^(0*)[1-9]$/; print "$Index\n";
      I was hoping I could incorporate the range ([0-9] or ^0) directly into the ‘index’ statement.
      $Index – index($Str1, ‘the first non-zero value’);
      but I guess not. So, it needs to be done in 2 statements.

      thanks,

      romano (aka KennB)
Re: index using a range
by choroba (Abbot) on Jun 13, 2013 at 23:07 UTC
    A bit cryptic, but without the need of $1:
    my $str = '000001'; my $index = length(($str =~ /^(0*)/)[0]);
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: index using a range
by vsespb (Hermit) on Jun 13, 2013 at 23:31 UTC
    Most cryptic:
    use List::Util qw/min/; $Str1 = "000001"; print min grep { $_ >= 0 } map { index $Str1, $_ } 1..9;
Re: index using a range
by hbm (Hermit) on Jun 14, 2013 at 00:00 UTC
    $Str1 = "000001"; #$Index = index($Str1, [1-9]); $Index = @{[$Str1=~/(?<=0)./g]}+0; print "$Index\n";

    Update: Doesn't work for "001001", for example...

Re: index using a range
by farang (Hermit) on Jun 14, 2013 at 06:22 UTC

    Assuming the string is only numerals and isn't all zeros (which could be checked for separately), here's a method that uses coercion of a string into a number.

    use strict; use warnings; for my $Str1 ( "987654", "087654", "007654", "000654", "000054", "0000 +04" ) { my $Index = length($Str1) - length($Str1 + 0) + 1; print "$Str1 pos=$Index\n"; } __END__ 987654 pos=1 087654 pos=2 007654 pos=3 000654 pos=4 000054 pos=5 000004 pos=6

Re: index using a range
by jwkrahn (Monsignor) on Jun 14, 2013 at 06:38 UTC
    $ perl -le' my $Str1 = "000001"; $Str1 =~ /[1-9]/ and my $Index = $-[ 0 ]; print "$Index\n"; ' 5
Re: index using a range
by hdb (Prior) on Jun 14, 2013 at 06:43 UTC

    A good application for the under-estimated comma operator:

    $str = "000001"; $index = ( $str =~ /^(0*)/, length $1 ); print "$index\n";
Re: index using a range
by LanX (Canon) on Jun 14, 2013 at 08:24 UTC
    > I know I can use pos or substitute for non-zero values and then do index but is there a way to do that directly?

    pos is the way to do it directly.

    DB<111> $str1 = "000001";$str1 =~ m/[^0]/g; print pos($str1) 6

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: index using a range
by trizen (Friar) on Jun 14, 2013 at 17:23 UTC
    Using index with tr/// :

        perl -E 'say index("000001000"=~tr/0/1/cr, "1")' # says "5"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038852]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (17)
As of 2014-12-19 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (85 votes), past polls