thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I need you wisdom once again. Although that I have found an answer to my question, I need your skills to help me understand why the output is formatted like this.
I want to split a user input based on the length of characters.
UpdateI modified the title, to actually describe better my question and to avoid confusion for future readers. The observation came from ww and thank you for the suggestion.
Sample of a working example is given bellow with the output as expected:
#! /usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $input = "1234567890abcdefghij0987654321ABCDEFGHIJlmnop"; chomp $input; # A "A text (ASCII) string, will be space padded." my @chunks = unpack("(A10)*", $input); print Dumper(\@chunks); __END__ $VAR1 = [ '1234567890', 'abcdefghij', '0987654321', 'ABCDEFGHIJ', 'lmnop' ];
While I was trying different solutions in order to reach to the solution I thought about using split and regex. The program seems to be working correctly but I do get blank lines, why? I can not understand where I am going wrong. I tried to use chomp but as it looks like there are no trailing "new line" characters. Does anyone understands why this is happening?
Sample of working code provided under:
#! /usr/local/bin/perl use strict; use warnings; use Data::Dumper; my $input = "1234567890abcdefghij0987654321ABCDEFGHIJlmnop"; chomp $input; my @chunks = split(/(.{10})/,$input); print Dumper(\@chunks); __END__ $VAR1 = [ '', '1234567890', '', 'abcdefghij', '', '0987654321', '', 'ABCDEFGHIJ', 'lmnop' ];
Thank you all for your time and effort reading and replying to my question.
|
|---|