in reply to Splitting two digits

If you want to split exactly two digits apart you could do:

my @field; if ($text =~ /^(\d)(\d)/) { @field = ($1, $2); }

This regex will find two (0-9) digit characters at the beginning of a string and save them to $1 and $2. If there is a match, the values will be assigned to the array @field. There's probably a more efficient way to do a split on digits only.

It's also worth noting that the previous suggestions are more flexible.

--
Grant me the wisdom to shut my mouth when I don't know what I'm talking about.

Replies are listed 'Best First'.
Re: Re: Splitting two digits
by cored (Scribe) on Jan 08, 2003 at 03:33 UTC
    I enter 3 number's with your code and only gave me the two first digits i think the code above can make the job
    #!/usr/bin/perl -lw $"=","; $_="20"; @F=split//; print "@F";

      My code only handles two digits by design. The title of the node is "Splitting two digits" so I restricted my solution to only returning two digits.

      --
      Grant me the wisdom to shut my mouth when I don't know what I'm talking about.