more useful options PerlMonks

### splitting a number into several numbers

by kidd (Curate)
 on Jul 05, 2002 at 12:54 UTC ( #179622=perlquestion: print w/ replies, xml ) Need Help??
kidd has asked for the wisdom of the Perl Monks concerning the following question:

Hello...

I have on little problem...

I have my number: 123345

BUT what I want is to split it up into three numbers: "12", "34" and "45"...

Hey, I know its easy, but I think Im just to dumb or naive for not to come with the solution...

• Comment on splitting a number into several numbers

Replies are listed 'Best First'.
Re: splitting
by Abigail-II (Bishop) on Jul 05, 2002 at 13:03 UTC
It's unclear what you really want. I mean, if you just have this number, and all you need is 12, 34 and 45, you could do:
```    my (\$var1, \$var2, \$var3) = (12, 34, 45);
But what do you want to do in the general case? Always end up with three numbers? Always get a list of 2 digit numbers?

In the former case, I'd use substr:

```my \$str = 123445;
my \$l = int (length (\$str) / 3);
my \$var1 = substr \$str, 0, \$l;
my \$var2 = substr \$str, \$l, \$l;
my \$var3 = substr \$str, 2 * \$l;
In the latter case, a regex will do:
```my @vars = \$str =~ /..?/g;
Next time, be more precise in what you want. And having a working example helps too (123345 isn't "12" . "34" . "45").

Abigail

Im sorry...I had a typo...I meant it to be 12 33 and 45...and yes I meant in a general case, and THAT was an example...

Re: splitting
by marvell (Pilgrim) on Jul 05, 2002 at 13:35 UTC

A nice simple match will cover it:

my (\$var1, \$var2, \$var3) = \$numberstring =~ /(\d{2})/g;

--
Steve Marvell

Re: splitting a number into several numbers
by blakem (Monsignor) on Jul 05, 2002 at 18:25 UTC
Although Abigail has provided a better answer, if you really wanted to use split you could:
```my @vars = grep length, split /(..?)/, \$str;

-Blake

```That's awesome Blake, like your solution :)
But what "grep length" does in here?

the following:
my @vars = split /(..?)/, \$str ;

return:
12 34 56
^ there is one space here, why?

and grep length get rid of the space, how that magic works?

-perlkid
```
Normally split is used to extract the stuff between delimiters. The delimiters are tossed aside, and the resulting list doesn't contain them. However, this behavior can be modified, and we can keep the delimiters if we want to:

From the split documentation:

```If the PATTERN contains parentheses, additional array elements are cre
+ated from each matching substring in the delimiter.

split(/([,-])/, "1-10,20", 3);

produces the list value

(1, '-', 10, ',', 20)

In my example, the delimiters are the parts we want to keep, the stuff between the delimiters is empty and needs to be thrown away. Without the grep, the list you're getting back is actually
```('', 12, '', 34, '', 56, '')
Where 12,34,56 are the delimiters matched by the regex and the empty strings are the "data" between the delimiters. Obviously those empty strings have got to go, so we get rid of them by grepping out things that have no length.

-Blake

Re: splitting a number into several numbers
by dwatson06 (Friar) on Jul 05, 2002 at 18:11 UTC
If this is input by the user and you will not know how many numbers will be coming in...
You can grab the length of the input...
half it...
do a loop using the value...
then just grab substrings by an increment of twos.
Of course you will need to make sure the number coming in is even then handle other conditions on the odds.
Hope this helps.
Daniel

Create A New User
Node Status?
node history
Node Type: perlquestion [id://179622]
Approved by claree0
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2016-07-01 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What is your favorite alternate name for a (specific) keyboard key?

Results (13 votes). Check out past polls.