RE: Writing files in reverse..and more
by mikfire (Deacon) on Jul 28, 2000 at 20:19 UTC
|
You kids are verbose. A round of Perl Golf, shall we?
print scalar( reverse substr( $_, 0, rindex($_," //") ) ) while (<>);
Update
davorg is correct - I fixed a few syntax errors. I had the
"\n" on the command line I used to test, but apparently
missed it when submitting the article. davorg's
solution is better than mine - the map is a nice touch.
mikfire | [reply] [d/l] |
|
Cheat! You just corrected the first problem that I saw (incorrect arguments to rindex).
The other problem is that it runs all of the lines together, you need to print "\n" at the end of each line like this:
print scalar( reverse substr( $_, 0, rindex($_," //") ) ), "\n" while
+(<>);
but if we're playing golf then you can save a couple of strokes by using map
print map { scalar reverse(substr( $_, 0, rindex($_," //") )), "\n" }
+<>;
--
<http://www.dave.org.uk>
European Perl Conference - Sept 22/24 2000, ICA, London
<http://www.yapc.org/Europe/> | [reply] [d/l] [select] |
|
A slightly more full featured try with a few more strokes (outside the map), using davorgs origional post meathod of spliting.
{
local $\="\n" => select STDOUT; # Or what ever FileHandle
map { print scalar reverse( (split m!\s+//!)[0] ) } <DATA>
}
Enjoy!
--
Casey
| [reply] [d/l] |
|
salut monkey
tu es con et encule
| [reply] |
Perhaps, perhaps, perhaps?
by gryng (Hermit) on Jul 28, 2000 at 18:56 UTC
|
while (<>) {
s/\W*\/+.*//;
print reverse $_
}
Not tested, but should work, no? (Sorry for the .* Ovid :) )
Ciao,
Gryn
Update: Escaped my slashes, opps. :) (Thanks fastolfe).
| [reply] [d/l] |
|
I would do the regexp like this: s~\s*//.*~~; Using a delimiter other than / keeps you from having to escape the slashes, otherwise it'd be like s/\s*\/\/.*//, which is kinda confusing. But yah the rest is just that simple. Except you aren't considering newlines:
while (<>) {
chomp;
s~\s*//.*~~;
print reverse $_;
print "\n";
}
| [reply] [d/l] [select] |
|
gryng: Actually, I recall mentioning that it's not always possible to avoid the dot star. I should have mentioned that sometimes it's a good choice. You found one of those "sometimes" :)
Cheers,
Ovid
| [reply] |
Re: Writing files in reverse..and more
by davorg (Chancellor) on Jul 28, 2000 at 18:56 UTC
|
open(IN, 'in.txt') or die "Can't open input file: $!\n";
open(OUT, '>out.txt') or die "Can't open output file: $!\n";
while (<IN>) {
my ($stuff, $dregs) = split m[ // ];
print OUT reverse($stuff), "\n";
}
--
<http://www.dave.org.uk>
European Perl Conference - Sept 22/24 2000, ICA, London
<http://www.yapc.org/Europe/> | [reply] [d/l] |
You da Monks!
by Anonymous Monk on Jul 28, 2000 at 20:39 UTC
|
Damn, you guys are good in perl. | [reply] |
Re: Writing files in reverse..and more
by lhoward (Vicar) on Jul 28, 2000 at 18:59 UTC
|
my $c="fred and deena // married";
$c=~s/\/\/.*$//;
print "".(reverse split //,$c)."\n";
| [reply] [d/l] |