Ninthwave has asked for the wisdom of the Perl Monks concerning the following question:
I have a string that is generated from a database. The string contains multiple records. The program that generates this string appends a two character code to the front of each string.
Example:
AA(<text values>), AB(<text values>), AC(<text values>)
Note the parentheses, commas and spaces are not part of the text values, but the text values themselves may contain commas and parentheses.
I am looking for the elegant way to capture each value, noting that the order is important as AA lines up with another dataset. Also note that so far I have seen this string with as many as 400+ values scrolling into QR and above though I have not seen if the system has any option for when the value exceeds ZZ so I used ZZ as my upper limit.
Also the reason I have an array for the sting value is the data field that this string comes from is an array though so far only the first element has been populated. I need more examples to determine if I will be able to change the section with @{$strings} to just $string in the future.
My solution - improvements or better algorithms sought
my $start = q{AA}; my $next = q{AB}; my $count = 0; my $value; foreach my $entry ( @{$strings} ) { if ( $count == $#$strings ) { $next = q{$}; } else { $next = q{\,\ } . _alpha_add($start); } $affiliation_string =~ m/$start\((.*?)\)($next)/s; $value = $1; $affiliation_key->{$entry} = $value; $start = _alpha_add($start); $next = _alpha_add($next); $count++; }
And the _alpha_add sub
sub _alpha_add { my $string = shift; if ( substr( $string, 1, 1 ) eq q{Z} ) { my $first_character = substr( $string, 0, 1 ); my $second_character = substr( $string, 1, 1 ); $first_character++; $second_character = q{A}; $string = $first_character . $second_character; } else { $string++; } return $string; }
UPDATE: Please note as a delimiter is most likely the wrong term, as the format prepends the data and is absent at the end of the string.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Character Text Delimiters
by AnomalousMonk (Archbishop) on Oct 26, 2011 at 00:22 UTC | |
|
Re: Character Text Delimiters
by RichardK (Parson) on Oct 25, 2011 at 16:31 UTC | |
by Ninthwave (Chaplain) on Oct 25, 2011 at 16:38 UTC | |
|
Re: Character Text Delimiters
by Sue D. Nymme (Monk) on Oct 25, 2011 at 18:39 UTC | |
by Ninthwave (Chaplain) on Oct 25, 2011 at 19:45 UTC | |
|
Re: Character Text Delimiters
by johngg (Canon) on Oct 26, 2011 at 10:52 UTC |