P
Peter Wyzl
I have been playing around with this for a while now and am clearly missing
something...
__SAMPLE__
#!/usr/bin/perl -w
use strict;
my $word = 'foobar';
my $string = substr $word,0,1;
$string = join '|', $string, substr $word,0,$_ for 2..length $word;
print $string;
__END SAMPLE__
prints 'f|fo|foo|foob|fooba|foobar' (sans quotes)
But!
changing to:
$string = join '|', substr$word,0,$_ for 1..length $word;
prints 'foobar'
Why is this discarding all but the last value from the list?
Whilst this:
$string = join '|', substr $word,0,1, substr$word,0,$_ for 2..length $word;
prints 'f'
why is this discarding all but the first value from the list?
Even more puzzling to me is why these are syntax errors:
$string = join '|', (substr $word,0,$_ for 1..length $word);
$string = join '|', [substr $word,0,$_ for (1..length $word)];
(and several other variants with braces)
Specifically I don't understand what the scalar variable $string is doing in
the first version that makes this work. I played with parens trying to
force list context but that is when I hit the syntax errors.
$string = join '|', (substr $word,0,$_ for (1..length $word));
causes the error:
syntax error at test.pl line 7, near "$_ for "
Scratching my head with that one...
something...
__SAMPLE__
#!/usr/bin/perl -w
use strict;
my $word = 'foobar';
my $string = substr $word,0,1;
$string = join '|', $string, substr $word,0,$_ for 2..length $word;
print $string;
__END SAMPLE__
prints 'f|fo|foo|foob|fooba|foobar' (sans quotes)
But!
changing to:
$string = join '|', substr$word,0,$_ for 1..length $word;
prints 'foobar'
Why is this discarding all but the last value from the list?
Whilst this:
$string = join '|', substr $word,0,1, substr$word,0,$_ for 2..length $word;
prints 'f'
why is this discarding all but the first value from the list?
Even more puzzling to me is why these are syntax errors:
$string = join '|', (substr $word,0,$_ for 1..length $word);
$string = join '|', [substr $word,0,$_ for (1..length $word)];
(and several other variants with braces)
Specifically I don't understand what the scalar variable $string is doing in
the first version that makes this work. I played with parens trying to
force list context but that is when I hit the syntax errors.
$string = join '|', (substr $word,0,$_ for (1..length $word));
causes the error:
syntax error at test.pl line 7, near "$_ for "
Scratching my head with that one...