-5

I have an JSON Body of an http post that has to be split into 80 character double quoted strings - but - whenever I use unpack to read the first 80 characters, the string pointer in the source string (which is not CR/LF delimited at the end of each line yet) never changes - e.g. the loop below keeps reading the same string over and over - I'm assuming that unpack is expecting a CR/LF to be pre-existing? What do I do if it isn't?

@row =unpack 'A80', $body;
foreach $line (@body)
{
    @row =unpack 'A80', $body;
    print '"'.$line.'"' ;
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • where is `@body` coming from? you need to supply a link as an example input, and code that actually works so we can test. do you have `use strict;` and `use warnings;` enabled? – stevieb Apr 22 '16 at 18:09
  • It's really unclear what you're trying to do. Anyway, there is no "string pointer" related to `unpack`. (Maybe you're thinking about `pos`, which is only used with regular expressions.) – cjm Apr 22 '16 at 18:10
  • If you're trying to parse JSON, you should use a [proper JSON parser](https://metacpan.org/pod/JSON). If that's not what you're trying to do, you should [edit] your question to clarify, it's pretty unclear right now. – ThisSuitIsBlackNot Apr 22 '16 at 18:21
  • Do you know Perl at all? Where has `$body` come from? – Borodin Apr 22 '16 at 18:24
  • @MaxLybbert: I agree. But unless you feel completely confident in your diagnosis (when you should simply post a solution) your comments should elicit more information. What more do you need to know to correct the OP's code? – Borodin Apr 22 '16 at 18:33

2 Answers2

1

It's really hard to understand your circumstance, but from your own "answer" it looks like you need this

my @groups = unpack '(a80)*', $body;

From your question it looks like this may be better

my @groups = unpack '(A80)*', $body;

But you really need to describe where $body came from, and what results you expect

Borodin
  • 126,100
  • 9
  • 70
  • 144
-2

Here's how I solved the problem:

my $n = 80;    # $n is group size.
my @groups = unpack "a$n" x (length( $body ) /$n ), $body;

# print @groups;
foreach $line (@groups) {
    print '"'.$line.'"' ;
}
Borodin
  • 126,100
  • 9
  • 70
  • 144