#!/usr/bin/perl -w use strict; #input print "Name of file to be inputted? :"; $infile=<>; print "What should the output file be named? The file will be automatically created. :"; $outfile=<>; chomp $infile; chomp $outfile; ##<<<<<<<<$outfile"; my @stack; while() { my @op = (); my $len = length($_); my $minsta = 0; my $minsto = 0; my $lastpos = 0; while(true) { $minsta = index $_,'{',$minsta; $minsto = index $_,'}',$minsto; last if ($len == $lastpos || ($minsta == -1 && $minsto == -1)); if ($minsta < $minsto) { my $nextsp = index $_,' ',$minsta; $nextsp = length($_) if ($nextsp == -1); my $stack = substr $_,$minsta+1,$nextsp-$minsta; push @op, (substr $_,$lastpos,$minsta-$lastpos), '<',$stack,'>'; push @stack, $stack; $lastpos = $minsta+1; } else { my $stack = pop @stack; die "Not balanced" unless (defined $stack); push @op, (substr $_,$lastpos,$minsto-$lastpos), ''; $lastpos = $minsto+1; } } print OUTFILE join "",@op,(substr $_,$lastpos,$len-$lastpos); } close INFILE; close FILEOUT; die "Not balanced" if (@stack); print "\nYour result is stored in file $outfile\nGoodbye.\n";