Tags:
create new tag
view all tags

SOLUZIONE ESERCIZIO IN PERL
#!/usr/local/bin/perl
use strict;
my ($fname, $laststr, $done, $cur1, $cur2, $line1, $line2);
if (@ARGV < 1) {
		  die("Usage: ",$0," file_name\n");
}
$fname=shift @ARGV;
open(FILEIN,$fname) || die("open $fname: $!\n");
$laststr=<FILEIN>;
$done=0;

while ($line2 = <FILEIN>) {
		  if ($line2 lt $laststr) {
					 $cur2=tell FILEIN;
					 last;
		  }
		  $laststr=$line2;
}
if (!defined($line2)) {
		  $done=1;
}
seek FILEIN,0,0;
while ($line1=<FILEIN>) {
		  if (($line1 lt $line2) || $done) {
					 print $line1;
		  } else {
					 $cur1=tell FILEIN;
					 while ($line1 gt $line2) {
								print $line2;
								seek FILEIN, $cur2, 0;
								if (eof(FILEIN)) {
										  $done=1;
										  last;
								} else {
										  $line2=<FILEIN>;
										  $cur2=tell FILEIN;
								  }
					 }
					 print $line1;
					 seek FILEIN, $cur1, 0;
			  }
			  last if ($line1 eq $laststr);
}

if (!$done) {
		  print $line2;
		  seek FILEIN, $cur2, 0;
		  while (<FILEIN>) {
					 print $_;
		  }
}

-- AntonioValletta - 15 Nov 2001

Topic revision: r1 - 2001-11-15 - AntonioValletta






 
Questo sito usa cookies, usandolo ne accettate la presenza. (CookiePolicy)
Torna al Dipartimento di Informatica
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2021 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback