in reply to split string by character count

SiLiK,
The following code should be all you want and a bag of chips too.
#!/usr/bin/perl use strict; use warnings; my @template = ( '1: date 6', '2: time 4', '3: duration 4', '4: calling-num 15', '5: dialed-num 18', '6: code-used 4', '7: cond-code 1', '8: ppm 5', '9: auth-code 13', ); my $string = '21090412500011 209 070279501 9047 +0 1111'; my %code = Parse_String(\@template, $string); print "$code{date}\n"; print "$code{'auth-code'}\n"; sub Parse_String { my ($rosetta, $string) = @_; my $template; my @fields; for ( @{ $rosetta } ) { if ( /^\d+:\s+([^\s]+)\s+(\d+)/ ) { push @fields , $1; $template .= 'a' . $2; } } my %code; @code{ @fields } = unpack $template , $string; return %code; }
I know I didn't provide you with another array because it appears that a hash would be better. It should be trivial to change it if that's what you truly want.

Cheers - L~R

Replies are listed 'Best First'.
Re^2: split string by character count
by SiLiK (Novice) on Sep 22, 2004 at 08:48 UTC
    10x L~R

    I decided to go with your solution since id made me re-read the chapter on working with arrays from my Perl book to understand it ;P

    many thanks to all !