in reply to Splitting a string in Perl

You're going to have a hard time using split to split this line (what if the hex is 20 20 20 - that'll give 3 spaces in ASCII, or what if the length of the data isn't a multiple of nine). You'll be better of using unpack.


Unless I state otherwise, my code all runs with strict and warnings

Replies are listed 'Best First'.
Re^2: Splitting a string in Perl
by kyle (Abbot) on Apr 26, 2008 at 19:07 UTC

    Good catch! To make a split solution work here, you'd need the optional third argument.

    use Data::Dumper; my $in = '0000 21 20 20 20 20 20 20 20 36 ! 6'; my @out = split /\s{2,}/, $in, 3; print Dumper \@out; __END__ $VAR1 = [ '0000', '21 20 20 20 20 20 20 20 36', '! 6' ];