Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi. For a string b019200184010, what is the easiest way to split on the first 0 leaving b, 19200184010 or string bhs019200184010 leaving bhs, 19200184010. Thanks.

Replies are listed 'Best First'.
Re: Split on first of several
by akho (Hermit) on May 29, 2009 at 21:24 UTC
Re: Split on first of several
by jwkrahn (Abbot) on May 29, 2009 at 21:27 UTC
    $ perl -le' for my $string ( "b019200184010", "bhs019200184010" ) { my ( $first, $second ) = split /0/, $string, 2; print "First: $first Second: $second"; } ' First: b Second: 19200184010 First: bhs Second: 19200184010
Re: Split on first of several
by ikegami (Patriarch) on May 29, 2009 at 22:49 UTC
    There's also
    my ($x, $y) = $s =~ /^(.*?)0(.*)/s; -or- my ($x, $y) = $s =~ /^([^0]+)0(.*)/s;
Re: Split on first of several
by bichonfrise74 (Vicar) on May 31, 2009 at 01:32 UTC
    Another possible way...
    #!/usr/bin/perl my $a = 'b019200184010'; my $counter = 0; for ( my $i = 0; $i <= length($a); $i++) { $counter++ if ( substr( $a, $i, 1) eq 0 ); print substr($a, $i, 1) if ( $counter > 0 ); }