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.#!/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; }
Cheers - L~R
In reply to Re: split string by character count
by Limbic~Region
in thread split string by character count
by SiLiK
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |