Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by ozgurp (Beadle)
on Feb 24, 2009 at 09:09 UTC ( [id://745942]=perlquestion: print w/replies, xml ) 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.
    Hows about the inelegant
    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

Log In?
Username:
Password:

What's my password?
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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found