I need to take a string like this:

UA-UI1,3,4,6

and expand it into an array of components like this:

UA1 UA3 UA4 UA6 UB1 UB3 UB4 UB6 ... UI1 UI3 UI4 UI6

(the above represents the most complex case, but I need to read
and expand several strings like this)

Can anyone suggest a simple clever way to do this? Or a package
that provides functionality that can be adapted to do this?

Failing that, I thought I might convert the above string into
a regular expression:

U[A-I][1346]

And use it to match the desired targets, but this is a much less
desirable approach (and it still looks nontrivial to make the conversion
to a regular expression...)


Your grammar is insufficiently precise to allow us to help you parse it.

Assuming you can parse it in to an array of arrayrefs of valid characters, like this:

my @elements = (['U'],['A'..'I'],['1','3','4','6']);

Then you can print it out like you want with something like this:

for my $i0 (@{$elements[0]}) {
  for my $i1 (@{$elements[1]}) {
    for my $i2 (@{$elements[2]}) {
      print "$i0$i1$i2\n";
    }
  }
}

which you can generate for any given size of @elements automatically with code like this:

my $script =
join('',map('for my $i'.$_.' (@{$elements['.$_.']}) {'."\n",(0..$#elements))).
qq(print ").join( '', map( '$i'.$_, (0..$#elements) ) ).qq(\\n";\n).
join( '', map( "}\n", (0..$#elements) ) );
eval $script;

In order to help you figure out how to parse

UA-UI1,3,4,6

into

my @elements = (['U'],['A'..'I'],['1','3','4','6']);


you have to more formally define your grammar.


For instance, how do you know the above maps to:

(UA-UI),(1,2,3,4)
and not
(U).(A-U).(I).(1,2,3,4)

Would 1,2-4,6 be legal?

Do you need to detect errors like 5-1 or 5--7 or 1,,3

Is , or - ever a legal character in itself?

Enjoy,
   Peter.

--
<http://www.interarchy.com/>  <http://documentation.interarchy.com/>

Reply via email to