Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Perl
Perl Misc
[newbie] chomp acting weird (or me not understanding how it works??)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Hendrik Maryns, post: 4781051"] Hi, I'm reading lines from an inputfile which contains on every line a word, a description of it, and the word again. I want to reconstruct the sentence these words formed. "LET" is the sign for punctuation, thus means the end of the sentence is reached. Whend i use chomp like this: #add this when testing open(INFILE, "test.txt"); # do{ my @zinwoorden; chomp($lijn=<INFILE>); while ($lijn!~/LET/){ $lijnnr++; push(@zinwoorden,$lijn); $lijn=<INBESTAND>; } #enduntil for (@zinwoorden){ s/(\w+).*/$1/; } my $zin=join (" ", @zinwoorden); print "$lijnnr \n$zin \n"; } until eof(INFILE); it doesn't work, just prints 0. If I do this, on the other hand: do{ my @zinwoorden; $lijn=<INFILE>; while ($lijn!~/LET/){ $lijnnr++; push(@zinwoorden,$lijn); $lijn=<INBESTAND>; } #enduntil for (@zinwoorden){ s/(\w+).*\n/$1/; # <-- workaround } my $zin=join (" ", @zinwoorden); print "$lijnnr \n$zin \n"; } until eof(INFILE); it works. I don't understand this. I'd like to use chomp, as it is safer, in case the \n would, by some strange event, not be there... I tried to change $/="\n", but as I don't really know what that does, I'm a bit afraid there, it didn't work anyway. An easy example of INFILE would be: ## start of test.txt dames N(soort,mv,basis) dame en VG(neven) en heren N(soort,mv,basis) heer meneer N(soort,ev,basis,zijd,stan) meneer de LID(bep,stan,rest) de rector N(soort,ev,basis,zijd,stan) rector een LID(onbep,stan,agr) een hele ADJ(prenom,basis,met-e,stan) heel mooie ADJ(prenom,basis,met-e,stan) mooi avond N(soort,ev,basis,zijd,stan) avond en VG(neven) en een LID(onbep,stan,agr) een hartelijk ADJ(vrij,basis,zonder) hartelijk welkom N(soort,ev,basis,onz,stan) welkom aan VZ(init) aan alle VNW(onbep,det,stan,prenom,met-e,agr) al aanwezigen ADJ(nom,basis,met-e,mv-n) aanwezig in VZ(init) in de LID(bep,stan,rest) de zaal N(soort,ev,basis,zijd,stan) zaal .. LET() . ## end of test.txt and a few more of these sentences would follow. Any comment on my code welcome too! Cheers, Hendrik [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Perl
Perl Misc
[newbie] chomp acting weird (or me not understanding how it works??)
Top