Re: Need help with a string parsing problem

2004-01-22 Thread Andy Turner
On Wed, Jan 21, 2004 at 10:39:09PM -0600, Ken Williams wrote:
> This is probably because 5.6 expands the whole for(...) list in 
> advance, but 5.8 evaluates it lazily.

Ah, but I'm using stock Perl on Panther, which is 5.8.1-RC3.  It also
happens on my Debian Unstable box which is running 5.8.2.

> In any case, it's always a little risky to use $1 and friends more than 
> 1 statement after the regex they come from.  Too many things clobber 
> 'em at a distance.

Yeah, that's my feeling.  And if the code ever becomes more complex then
their meaning can become pretty obscure.

-- 
Andy <[EMAIL PROTECTED]> - http://anime.mikomi.org/ - Community Anime Reviews 
  Good men, if such there be, would either remain true to their political
  faith and lose their economic support, or they would cling to their
  economic master and be utterly unable to do the slightest good. The
  political arena leaves one no alternative, one must either be a dunce or
  a rogue.-- Emma Goldman


Re: Need help with a string parsing problem

2004-01-21 Thread Ken Williams
On Sunday, January 18, 2004, at 02:38  PM, Andy Turner wrote:

There some sort of regexp strangeness going on here that I can't grok.
Your script doesn't work for me unless I print out the values of $1 
AND $2.
If I just print out one it doesn't work either.

turner:~$ cat foo.pl
$_ = "UA-UI1,3,4,6";
m~(..)\-(..)(\d.+)~;
for $CC( $1 .. $2 ) { for $n ( split /,/, $3 ) { print "$CC$n "} }
turner:~$ perl foo.pl
01 03 04 06 turner:~$
But oddly, it works if I add a little debuging:

turner:~$ cat foo.pl
$_ = "UA-UI1,3,4,6";
m~(..)\-(..)(\d.+)~;
print "$1, $2\n";
for $CC( $1 .. $2 ) { for $n ( split /,/, $3 ) { print "$CC$n "} }
turner:~$ perl foo.pl
UA, UI
UA1 UA3 UA4 UA6 UB1 UB3 UB4 UB6 UC1 UC3 UC4 UC6 UD1 UD3 UD4 UD6 UE1 
UE3 UE4 UE6
UF1 UF3 UF4 UF6 UG1 UG3 UG4 UG6 UH1 UH3 UH4 UH6 UI1 UI3 UI4 UI6 
turner:~$
This is probably because 5.6 expands the whole for(...) list in 
advance, but 5.8 evaluates it lazily.

Or it could be because the $1 and $2 variables weren't being properly 
detected as strings by the .. operator in 5.6 unless they had been used 
in a string context.  But they should pretty much always be strings, 
and I'm not sure I've seen this problem quite like this.

In any case, it's always a little risky to use $1 and friends more than 
1 statement after the regex they come from.  Too many things clobber 
'em at a distance.  To be safer, I'd write that as:

$_ = "UA-UI1,3,4,6";
my ($start, $stop, $digits) = m~(..)\-(..)(\d.+)~;
for $CC( $start .. $stop ) { for $n ( split /,/, $digits ) { print 
"$CC$n "} }

 -Ken



Re: Need help with a string parsing problem

2004-01-18 Thread John Delacour
At 11:03 pm + 18/1/04, John Delacour wrote:

Hey, wait a minute --  maybe I was :-)  I get your problem if I run 
it in MacPerl with 5.6.1...


It works in 5.6.1 if I quote the matches :

$_ = "UA-UI1,3,4,6";
/  (\w\w)-(\w\w)(\d.+)  /x;
for ("$1".."$2" ) {for $n ( split /[^\d*]/, $3 ) { print "$_$n "}}


Re: Need help with a string parsing problem

2004-01-18 Thread John Delacour
At 3:38 pm -0500 18/1/04, Andy Turner wrote:

There some sort of regexp strangeness going on here that I can't grok.
Your script doesn't work for me unless I print out the values of $1 AND $2.
If I just print out one it doesn't work either.
I can't say.  I've tried it in BBEdit, in another Perl editor, in 
Eudora using 'do shell script ...' and in the Terminal and it works 
fine in all.

I ought to say that I'm using Perl 5.8.3, but I certainly was not 
aware that I was using some great new functionality.

Hey, wait a minute --  maybe I was :-)  I get your problem if I run 
it in MacPerl with 5.6.1 and, as you say, it works if I add  push 
@harder, "$1$2"; before the loop.

JD



Re: Need help with a string parsing problem

2004-01-18 Thread Andy Turner
There some sort of regexp strangeness going on here that I can't grok.
Your script doesn't work for me unless I print out the values of $1 AND $2. 
If I just print out one it doesn't work either.

turner:~$ cat foo.pl
$_ = "UA-UI1,3,4,6";
m~(..)\-(..)(\d.+)~;
for $CC( $1 .. $2 ) { for $n ( split /,/, $3 ) { print "$CC$n "} }
turner:~$ perl foo.pl
01 03 04 06 turner:~$ 

But oddly, it works if I add a little debuging:

turner:~$ cat foo.pl
$_ = "UA-UI1,3,4,6";
m~(..)\-(..)(\d.+)~;
print "$1, $2\n";
for $CC( $1 .. $2 ) { for $n ( split /,/, $3 ) { print "$CC$n "} }
turner:~$ perl foo.pl
UA, UI
UA1 UA3 UA4 UA6 UB1 UB3 UB4 UB6 UC1 UC3 UC4 UC6 UD1 UD3 UD4 UD6 UE1 UE3 UE4 UE6
UF1 UF3 UF4 UF6 UG1 UG3 UG4 UG6 UH1 UH3 UH4 UH6 UI1 UI3 UI4 UI6 turner:~$

-- 
Andy <[EMAIL PROTECTED]> - http://anime.mikomi.org/ - Community Anime Reviews
   'Course, that doesn't work when 'a' contains parentheses.
-- Larry Wall in <[EMAIL PROTECTED]>


Re: Need help with a string parsing problem

2004-01-18 Thread John Delacour
At 10:19 pm -0800 17/1/04, Kim Helliwell wrote:

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
How about this:

$_ = "UA-UI1,3,4,6";
m~(..)\-(..)(\d.+)~;
for $CC( $1 .. $2 ) { for $n ( split /,/, $3 ) { print "$CC$n "} }
UA1 UA3 UA4 UA6 UB1 UB3 UB4 UB6 UC1 UC3 UC4 UC6 UD1 UD3 UD4 UD6 UE1 
UE3 UE4 UE6 UF1 UF3 UF4 UF6 UG1 UG3 UG4 UG6 UH1 UH3 UH4 UH6 UI1 UI3 
UI4 UI6


Re: Need help with a string parsing problem

2004-01-18 Thread Andy Turner
On Sat, Jan 17, 2004 at 10:19:24PM -0800, Kim Helliwell wrote:
> 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

How's this?

my $str = "UA-UI1,3,4,6";
if ( my( $from, $to, $nums ) = ( $str =~ /^(\w+)-(\w+)(\d(?:,\d)*)?$/ ) ) {
my @nums = split /,/, $nums;
for ( my $chars = $from; $chars le $to; $chars ++ ) {
foreach ( @nums ) {
print "$chars$_\n";
}
}
}
else {
   die "Couldn't parse $str!\n";
}

-- 
Andy <[EMAIL PROTECTED]> - http://anime.mikomi.org/ - Community Anime Reviews
   It's easy to fall into the habit of choosing rigor over vigor. [...]  We
already have lots of computer languages with rigor, but not so many with
vigor.  -- Larry Wall in <[EMAIL PROTECTED]>


Re: Need help with a string parsing problem

2004-01-18 Thread Peter N Lewis
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.
--
  


Need help with a string parsing problem

2004-01-17 Thread Kim Helliwell
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...)
Any help appreciated.

Kim Helliwell
[EMAIL PROTECTED]
Homepage: http://homepage.mac.com/kimgh