### How to divide a line from a text file into 8 digit chunks

 on Feb 24, 2009 at 09:09 UTC Need Help??

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

Hi, It has been five years, now I am a bit rusty but hopefully it will remember everything, I tried to solve this problem myself but did not work. I have a text file and I need to read each line and devide it into 8 digit chunks, and put it into an array. For example, I would like to put the following line into 8 digit chunks but the last chunck is shorter than eight digit so it has to be 7 in this case. Th last chunk can be between 1 and 8 digits:
```GRID     213736         15235.2 -2606.28596.178

\$array[0] = "GRID    "
\$array[1] = " 213736 "
\$array[3] = "        "
\$array[4] = "15235.2 "
\$array[5] = "-2606.28"
\$array[6] = "596.178"
Could you help? Thanks.

Replies are listed 'Best First'.
Re: How to divide a line from a text file into 8 digit chunks
by BrowserUk (Patriarch) on Feb 24, 2009 at 09:14 UTC
Re: How to divide a line from a text file into 8 digit chunks
by moritz (Cardinal) on Feb 24, 2009 at 09:18 UTC
Another possibility is
```my @array = split m/(.{8})/, \$string;
@array = grep length, @array

Or a bit nicer

```push @array, \$1 while \$str =~ m/(.{1,8})/g;

TIMTOWTDI (though I personally prefer the unpack solution)

(Update: tested and fixed the solutions)

Hi I tried both unpack and your suggestion,
```my @array = split m/(.{8})/, \$string;
seems to create a size 11 array with some additional spaces whereas unpack creates a size 6 array. But I am not sure why it does that.
seems to create a size 11 array with some additional spaces

This is why I added the line with grep after that.

Re: How to divide a line from a text file into 8 digit chunks
by balakrishnan (Monk) on Feb 24, 2009 at 10:40 UTC
You might be the interested in the following.
```open FH,"./input.txt";

while(<FH>) {
@arr = (\$_ =~ m/.{1,8}/g);
print join "***\n",@arr;
}
Re: How to divide a line from a text file into 8 digit chunks
by Anonymous Monk on Feb 24, 2009 at 11:59 UTC
This looks like a fixed field length line cut.
The \$array[2] line is missing, I am assuming a typo.
```cat text | perl -ne 'chomp ;
foreach \$i(0..length(\$_)%7) {\$x[\$i]=substr(\$_,\$i*8,8)};
foreach \$i(0..length(\$_)%7) {print "\\$array[\$i]",\$x[\$i],
+"<\n"}'
with Perl v5.8.1 on text that contains .. GRID 213736 15235.2 -2606.28596.178
```\$array[0]GRID    <
\$array[1] 213736 <
\$array[2]        <
\$array[3]15235.2 <
\$array[4]-2606.28<
\$array[5]596.178<
Gannett
Re: How to divide a line from a text file into 8 digit chunks
by Anonymous Monk on Feb 24, 2009 at 11:53 UTC
The \$array[2] line is missing, I am assuming a typo. Hows about the inelegant This looks like a fixed field length line cut
```cat text | perl -ne 'chomp ;
foreach \$i(0..length(\$_)%7) {\$x[\$i]=substr(\$_,\$i*8,8)};
foreach \$i(0..length(\$_)%7) {print "\\$array[\$i]",\$x[\$i],
+"<\n"}'
with Perl v5.8.1 on text that contains ..
```GRID     213736         15235.2 -2606.28596.178
```\$array[0]GRID    <
\$array[1] 213736 <
\$array[2]        <
\$array[3]15235.2 <
\$array[4]-2606.28<
\$array[5]596.178<
Gannett

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2022-05-25 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you prefer to work remotely?

Results (90 votes). Check out past polls.

Notices?