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

I'm a little new to perl, and I'm trying to figure out how to accomplish this sort of thing: If I have a text string such that

$text = "20"

how can I split it up so that

@field[0] = 2, @field[1] = 0

I'm sure this is quite easy in perl, I'm just having a little trouble figuring out how to do it. Any help?

Fixed square brackets - dvergin 2003-01-07

Replies are listed 'Best First'.
Re: Splitting two digits
by Zaxo (Archbishop) on Jan 08, 2003 at 00:59 UTC

    You want to split on the empty string or pattern: my @fields = split '', $text;

    After Compline,
    Zaxo

      You want to split on the empty string or pattern
      Just a quick note while the subject has been bought up - a string passed to split is auto-magically converted into a regex, with the sole exception of providing a q{ } (a single space) as the pattern (see. the docs for more info on this behaviour). This ambiguity was raised on p5p recently and the discussion can be followed here.
      HTH

      _________
      broquaint

Re: Splitting two digits
by LTjake (Prior) on Jan 08, 2003 at 01:01 UTC
    I've used split like this:
    @field = split //, $text;
    to accomplish the task. Note that it will split up ALL chars of $text, not just numbers -- and not just two of them.

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich
Re: Splitting two digits
by Ionizor (Pilgrim) on Jan 08, 2003 at 01:59 UTC

    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.

      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.

Re: Splitting two digits
by fredopalus (Friar) on Jan 08, 2003 at 04:24 UTC
    You could try:
    $text="20"; $field[0] = substr($text, 0,1); $field[1] = substr($text, 1,2);

    Notice that i use $field[0] instead of @field[0]. This is because you're only referring to a single element in the array, and not to the entire array.

      I knew there was a better way. Here's a general function for splitting the first n characters of a string into an array, trapping to make sure they are digits:

      my @field; # Set to desired number of chars or switch for next line # if you want the whole string. my $numchars = 2; # my $numchars = length($text); for(my $i = 0; $i < $numchars; $i++) { my $curchar = substr($text, $i, 1); if ($curchar =~ /\d/) { push @field, $curchar; } }

      Also, substr($text, 1,2); should be substr($text, 1,1); in the above.

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

        Why not combine the various answers in this thread? :)

        my @digits = grep /\d/, split //, substr($str, 0, $len);

        However, I'd probably do

        my @digits = substr($str, 0, $len) =~ /\d/g;

        ihb