in reply to obtaining the three leftmost characters of a string

If you're sure that - or some other delimeter will always be present and in the same position, split would suit your example...

# left = 555, right = 4587 my($left,$right) = split( /-/ => $string, 2 );

If you really wanted only the first three regardless of content, then take a look at substr...

# left = 555, string = 555-4587 my($left) = substr( $string => 0, 3 ); # leftmost = 555, string = -4587 my($left) = substr( $string => 0, 3, "" );

    --k.


Replies are listed 'Best First'.
•Fat Arrow abuse (was Re: Re: obtaining the three leftmost characters of a string)
by merlyn (Sage) on Jun 10, 2002 at 14:33 UTC
    Please do not use arrow as a synonym for comma except in the privacy of your own cubicle. Otherwise, in the most general case, it can lead to breakage. Witness:
    @things = split /,/ => $input; # works use constant DELIMITER => ','; @things = split DELIMITER => $input; # BREAKS (treats DELIMITER as a q +uoted word) @things = split DELIMITER, $input; # works
    Fat arrow should be used only when the quoting powers are wanted, and possibly to set up hash key/value pairs, paying special care to in-scope "use constants".

    -- Randal L. Schwartz, Perl hacker