[perl] zapisywanie danych do pliku
Napisz skrypt wczytujący z konsoli łańcuch znaków do tablicy znaków. Przyjmijmy, że wczytywany łańcuch liczy nie więcej niż 30 znaków (skrypt tego sprawdzać nie musi). Skrypt ma zapisywać w pliku tekstowym w kolejnych wierszach pod-łańcuch (podzbiór znaków) wczytanego łańcucha, rozpoczynający się od pierwszego znaku tego łańcucha i o liczbie znaków oryginalnego łańcucha wzrastającej o jeden w kolejnych wierszach. Przykład dla „ala ma kota”: wiersz-01 „a” wiersz-05 „ala m” wiersz-09 „ala ma ko” wiersz-02 „al” wiersz-06 „ala ma” wiersz-10 „ala ma kot” wiersz-03 „ala” wiersz-07 „ala ma ” wiersz-11 „ala ma kota” wiersz-04 „ala ” wiersz-08 „ala ma k” Nazwa wynikowego pliku tekstowego może być zawarta w kodzie źródłowym.
#!/usr/bin/perl use warnings; print "Podaj łańcuch znaków: "; $x = <STDIN>; open(PLIK, "> plik.txt") or die "Nie można otworzyć pliku"; for($i = 0; $i < length($x); $i++) { print PLIK substr($x, 0, $i). "n"; } close(PLIK);
2 komentarze/y
Napisz komentarz
Komentarze są moderowane. Po akceptacji przez administratora zostaną wyświetlone na stronie.

22/04/2010 o 15:35
Trochę się czepię, otóż taki sposób obsługi plików, choć spotykany w praktycznie wszystkich kursach w sieci, jest odradzany. Jest tu też parę innych rzeczy, wlepię kawałek loga perlcritica dla powyższego kodu:
rgawron@foo:/tmp$ perlcritic -1 ./x.pl
Code is not tidy at line 1, column 1. See page 33 of PBP. (Severity: 1)
RCS keywords $Id$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2)
RCS keywords $Revision$, $HeadURL$, $Date$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2)
RCS keywords $Revision$, $Source$, $Date$ not found at line 1, column 1. See page 441 of PBP. (Severity: 2)
No „VERSION” variable found at line 1, column 1. See page 404 of PBP. (Severity: 2)
Found „N{SPACE}” at the end of the line at line 3, column 1. Don’t use whitespace at the end of lines. (Severity: 1)
Return value of flagged function ignored – print at line 4, column 1. See pages 208,278 of PBP. (Severity: 1)
Code before strictures are enabled at line 4, column 1. See page 429 of PBP. (Severity: 5)
Useless interpolation of literal string at line 4, column 7. See page 51 of PBP. (Severity: 1)
Use „” or „” or a prompting module instead of „” at line 5, column 6. See pages 216,220,221 of PBP. (Severity: 4)
Found „N{SPACE}” at the end of the line at line 6, column 1. Don’t use whitespace at the end of lines. (Severity: 1)
Builtin function called with parens at line 7, column 1. See page 13 of PBP. (Severity: 1)
Bareword file handle opened at line 7, column 1. See pages 202,204 of PBP. (Severity: 5)
Two-argument „open” used at line 7, column 1. See page 207 of PBP. (Severity: 5)
Useless interpolation of literal string at line 7, column 12. See page 51 of PBP. (Severity: 1)
„die” used instead of „croak” at line 7, column 29. See page 283 of PBP. (Severity: 3)
Useless interpolation of literal string at line 7, column 33. See page 51 of PBP. (Severity: 1)
Found „N{SPACE}” at the end of the line at line 8, column 1. Don’t use whitespace at the end of lines. (Severity: 1)
C-style „for” loop used at line 9, column 4. See page 100 of PBP. (Severity: 2)
Builtin function called with parens at line 9, column 18. See page 13 of PBP. (Severity: 1)
File handle for „print” is not braced at line 11, column 3. See page 217 of PBP. (Severity: 1)
Return value of flagged function ignored – print at line 11, column 3. See pages 208,278 of PBP. (Severity: 1)
Found „N{SPACE}” at the end of the line at line 13, column 1. Don’t use whitespace at the end of lines. (Severity: 1)
Builtin function called with parens at line 14, column 1. See page 13 of PBP. (Severity: 1)
Return value of „close” ignored at line 14, column 1. Check the return value of „close” for success. (Severity: 2)
Return value of flagged function ignored – close at line 14, column 1. See pages 208,278 of PBP. (Severity: 1)
IMHO nie wszystkie ostrzeżenia z tego narzędzia są istotne ale warto nim sprawdzać bo znajduje rzeczywiste błędy.
22/04/2010 o 16:21
dzięki za zwrócenie uwagi na ten aspekt