Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sample program from Programming Perl (Camel book) not running as expected

Reply
Thread Tools

Sample program from Programming Perl (Camel book) not running as expected

 
 
hiabhijeet@gmail.com
Guest
Posts: n/a
 
      12-24-2012
I am trying to execute the following sample program on WINDOWS but its not executing as expected. Its only reading the first line from the file and displaying it. Its not reading the rest of the lines -

open(GRADES, "grades") or die "Can't open grades: $!\n";
while(defined($line = <GRADES>)) {
print "[1] - $line\n";
($student, $grade) = split(" ", $line);
$grades{$student} .= $grade . " ";
print "[2] - $student\t$grade\n";
}

foreach $student (sort keys %grades) {
print "[3] - $student\n";
$scores = 0;
$total = 0;
@grades = split(" ", $grades{$student});
foreach $grade (@grades) {
$total += $grade;
$scores++;
}
$average = $total / $scores;
print "$student: $grades{$student}\tAverage: $average\n";
}

Please note - "grades" is a file that contains below data -

NoŽl 25
Ben 76
Clementine 49
Norm 66
Chris 92
Doug 42
Carol 25
Ben 12
Clementine 0
Norm 66
 
Reply With Quote
 
 
 
 
Michael Vilain
Guest
Posts: n/a
 
      12-24-2012
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I am trying to execute the following sample program on WINDOWS but its not
> executing as expected. Its only reading the first line from the file and
> displaying it. Its not reading the rest of the lines -
>
> open(GRADES, "grades") or die "Can't open grades: $!\n";
> while(defined($line = <GRADES>)) {
> print "[1] - $line\n";
> ($student, $grade) = split(" ", $line);
> $grades{$student} .= $grade . " ";
> print "[2] - $student\t$grade\n";
> }
>
> foreach $student (sort keys %grades) {
> print "[3] - $student\n";
> $scores = 0;
> $total = 0;
> @grades = split(" ", $grades{$student});
> foreach $grade (@grades) {
> $total += $grade;
> $scores++;
> }
> $average = $total / $scores;
> print "$student: $grades{$student}\tAverage: $average\n";
> }
>
> Please note - "grades" is a file that contains below data -
>
> NoŽl 25
> Ben 76
> Clementine 49
> Norm 66
> Chris 92
> Doug 42
> Carol 25
> Ben 12
> Clementine 0
> Norm 66


Check the line endings on the file. I don't know if the Windows Perl
implementation uses "\n" as a line ending as UNIX and MacOS versions of
Perl or "\r\n" for Windows standard. How do you know what is what?
That depends on the editor you're using, I think.

--
DeeDee, don't press that button! DeeDee! NO! Dee...
[I filter all Goggle Groups posts, so any reply may be automatically ignored]


 
Reply With Quote
 
 
 
 
hiabhijeet@gmail.com
Guest
Posts: n/a
 
      12-24-2012
On Monday, 24 December 2012 12:08:33 UTC+8, Michael Vilain wrote:
> In article <(E-Mail Removed)>, (E-Mail Removed) wrote: > I am trying to execute the following sample program on WINDOWS but its not > executing as expected. Its only reading the first line from the file and > displaying it. Its not reading the rest of the lines - > > open(GRADES, "grades") or die "Can't open grades: $!\n"; > while(defined($line = <GRADES>)) { > print "[1] - $line\n"; > ($student, $grade) = split(" ", $line); > $grades{$student} .= $grade . " "; > print "[2] - $student\t$grade\n"; > } > > foreach $student (sort keys %grades) { > print "[3] - $student\n"; > $scores = 0; > $total = 0; > @grades = split(" ", $grades{$student}); > foreach $grade (@grades) { > $total += $grade; > $scores++; > } > $average = $total / $scores; > print "$student: $grades{$student}\tAverage: $average\n"; > } > > Please note - "grades" is afile that contains below data - > > NoŽl 25 > Ben 76 > Clementine 49 > Norm 66 > Chris 92 > Doug 42 > Carol 25 > Ben 12 > Clementine 0 > Norm 66 Check the line endings on the file. I don't know if the Windows Perl implementation uses "\n" as a line ending as UNIX and MacOS versions of Perl or "\r\n" for Windows standard. How do you know what is what? That depends on theeditor you're using, I think. -- DeeDee, don't press that button! DeeDee! NO! Dee... [I filter all Goggle Groups posts, so any reply may be automatically ignored]


Thanks. Thats what.. what would make this program work on windows. I used UltraEdit to convert this file to DOS.
 
Reply With Quote
 
hiabhijeet@gmail.com
Guest
Posts: n/a
 
      12-24-2012
Thanks. Yes... would like to know what would make this program work on Windows. About the file format - I used UltraEdit to save this file in DOS mode.
 
Reply With Quote
 
hiabhijeet@gmail.com
Guest
Posts: n/a
 
      12-24-2012
Hey.. got this to work. Earlier I had copy pasted the file contents from the book pdf to notepad. Now reworte the data from scratch in a new file (using UltraEdit) and saved it as it is. Now program works fine!!
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      12-24-2012
Michael Vilain <(E-Mail Removed)> writes:
> (E-Mail Removed) wrote:
>
>> I am trying to execute the following sample program on WINDOWS but its not
>> executing as expected. Its only reading the first line from the file and
>> displaying it. Its not reading the rest of the lines -
>>


[...]

> Check the line endings on the file. I don't know if the Windows Perl
> implementation uses "\n" as a line ending as UNIX and MacOS versions of
> Perl or "\r\n" for Windows standard.


This is supposed to be transparent for files opened in text mode (the
default): A WinDOS/2-perl should translate \xd\xa to 'a logical \n' on
input and translate this 'logical \n' back to \xd\xa on output except
if binmode was used to disable this behaviour.

NB: I haven't tested this.
 
Reply With Quote
 
MeV
Guest
Posts: n/a
 
      12-25-2012
On Monday, December 24, 2012 11:50:50 AM UTC-8, Rainer Weikusat wrote:
> Michael Vilain <(E-Mail Removed)> writes:
>
> > (E-Mail Removed) wrote:
> >
> >> I am trying to execute the following sample program on WINDOWS but itsnot
> >> executing as expected. Its only reading the first line from the file and
> >> displaying it. Its not reading the rest of the lines -
> >>

>
>
>
> [...]
>
> > Check the line endings on the file. I don't know if the Windows Perl
> > implementation uses "\n" as a line ending as UNIX and MacOS versions of
> > Perl or "\r\n" for Windows standard.

>
> This is supposed to be transparent for files opened in text mode (the
> default): A WinDOS/2-perl should translate \xd\xa to 'a logical \n' on
> input and translate this 'logical \n' back to \xd\xa on output except
> if binmode was used to disable this behaviour.
>
>
>
> NB: I haven't tested this.


It definitely isn't transparent on MacOS X. If you open a Windows-formatted file in MacOS X perl, you get the exact behavior he described. That's what twigged me to the problem. If you change the file to UNIX line-endings,the problem goes away and perl works as expected.

Don't know what editors there are on Windows that support setting or changing the line-endings. vi on Unix will tell you if a Windows file has ^M line endings. BBEdit and Textwrangler on MacOS supports this trivially.
 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      12-25-2012
MeV <(E-Mail Removed)> writes:
> On Monday, December 24, 2012 11:50:50 AM UTC-8, Rainer Weikusat wrote:
>> Michael Vilain <(E-Mail Removed)> writes:
>>
>> > (E-Mail Removed) wrote:
>> >
>> >> I am trying to execute the following sample program on WINDOWS but its not
>> >> executing as expected. Its only reading the first line from the file and
>> >> displaying it. Its not reading the rest of the lines -
>> >>

>>
>>
>>
>> [...]
>>
>> > Check the line endings on the file. I don't know if the Windows Perl
>> > implementation uses "\n" as a line ending as UNIX and MacOS versions of
>> > Perl or "\r\n" for Windows standard.

>>
>> This is supposed to be transparent for files opened in text mode (the
>> default): A WinDOS/2-perl should translate \xd\xa to 'a logical \n' on
>> input and translate this 'logical \n' back to \xd\xa on output except
>> if binmode was used to disable this behaviour.
>>
>>
>>
>> NB: I haven't tested this.

>
> It definitely isn't transparent on MacOS X. If you open a
> Windows-formatted file in MacOS X perl, you get the exact behavior
> he described. That's what twigged me to the problem. If you change
> the file to UNIX line-endings, the problem goes away and perl works
> as expected.


Well, a MAC OS X perl is supposed to handle MAC OS X text files
'transparently' (suprising, ain't int?) in the same way a Windows perl
is supposed to handle Windows text files transparently ...
 
Reply With Quote
 
Michael Vilain
Guest
Posts: n/a
 
      12-25-2012
In article <(E-Mail Removed) >,
Rainer Weikusat <(E-Mail Removed)> wrote:

> MeV <(E-Mail Removed)> writes:
> > On Monday, December 24, 2012 11:50:50 AM UTC-8, Rainer Weikusat wrote:
> >> Michael Vilain <(E-Mail Removed)> writes:
> >>
> >> > (E-Mail Removed) wrote:
> >> >
> >> >> I am trying to execute the following sample program on WINDOWS but its
> >> >> not
> >> >> executing as expected. Its only reading the first line from the file
> >> >> and
> >> >> displaying it. Its not reading the rest of the lines -
> >> >>
> >>
> >>
> >>
> >> [...]
> >>
> >> > Check the line endings on the file. I don't know if the Windows Perl
> >> > implementation uses "\n" as a line ending as UNIX and MacOS versions of
> >> > Perl or "\r\n" for Windows standard.
> >>
> >> This is supposed to be transparent for files opened in text mode (the
> >> default): A WinDOS/2-perl should translate \xd\xa to 'a logical \n' on
> >> input and translate this 'logical \n' back to \xd\xa on output except
> >> if binmode was used to disable this behaviour.
> >>
> >>
> >>
> >> NB: I haven't tested this.

> >
> > It definitely isn't transparent on MacOS X. If you open a
> > Windows-formatted file in MacOS X perl, you get the exact behavior
> > he described. That's what twigged me to the problem. If you change
> > the file to UNIX line-endings, the problem goes away and perl works
> > as expected.

>
> Well, a MAC OS X perl is supposed to handle MAC OS X text files
> 'transparently' (suprising, ain't int?) in the same way a Windows perl
> is supposed to handle Windows text files transparently ...


There's a big difference between 'supposed to' and actuality. Kinda
like the architect's plans and the builder's 'built-as' drawings.

Googling for 'perl windows line endings' gives lots of solutions to this
problem. But if the OP didn't know there was a problem to begin with,
then they'd be mystified by why Windows Perl doesn't work the same as
documented in the Camel book.

I had to write a perl script to convert and import a CSV file into MySQL
but I never new where that script would be coming from. So I had to
build the line-ending detection into my code. I think it was quite easy
once you know that's an issue.

--
DeeDee, don't press that button! DeeDee! NO! Dee...
[I filter all Goggle Groups posts, so any reply may be automatically ignored]


 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      12-25-2012
Michael Vilain <(E-Mail Removed)> writes:
> In article <(E-Mail Removed) >,
> Rainer Weikusat <(E-Mail Removed)> wrote:
>> MeV <(E-Mail Removed)> writes:


[...]

>> >> >> I am trying to execute the following sample program on WINDOWS but its
>> >> >> not
>> >> >> executing as expected. Its only reading the first line from the file
>> >> >> and
>> >> >> displaying it. Its not reading the rest of the lines -
>> >> >>
>> >>
>> >>
>> >>
>> >> [...]
>> >>
>> >> > Check the line endings on the file. I don't know if the Windows Perl
>> >> > implementation uses "\n" as a line ending as UNIX and MacOS versions of
>> >> > Perl or "\r\n" for Windows standard.
>> >>
>> >> This is supposed to be transparent for files opened in text mode (the
>> >> default): A WinDOS/2-perl should translate \xd\xa to 'a logical \n' on
>> >> input and translate this 'logical \n' back to \xd\xa on output except
>> >> if binmode was used to disable this behaviour.
>> >>
>> >>
>> >>
>> >> NB: I haven't tested this.
>> >
>> > It definitely isn't transparent on MacOS X. If you open a
>> > Windows-formatted file in MacOS X perl, you get the exact behavior
>> > he described. That's what twigged me to the problem. If you change
>> > the file to UNIX line-endings, the problem goes away and perl works
>> > as expected.

>>
>> Well, a MAC OS X perl is supposed to handle MAC OS X text files
>> 'transparently' (suprising, ain't int?) in the same way a Windows perl
>> is supposed to handle Windows text files transparently ...

>
> There's a big difference between 'supposed to' and actuality. Kinda
> like the architect's plans and the builder's 'built-as' drawings.


In this case, this has been documented functionality since
I-don't-know-when (presumably, since a Perl port of Windows existed)
and consequently, the chances that a _Windows_ perl can't handle
_Windows_ text files despite the documentation says it can are IMO
very slim. OTOH, a _UNIX(*)_ perl won't be able to handle 'Windows text
files' in 'text mode' because it expects _UNIX(*)_ text files ...

> Googling for 'perl windows line endings' gives lots of solutions to this
> problem.


.... which happens to be consistent with your observation of a certain
(Free)BSD bastard and all of these "someone wrote this with notepad
and while it works on my laptop, it doesn't work on the webserver !!1"
tips.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation program not returning expected output Logan Lee C Programming 15 12-19-2007 04:48 AM
looking for asp.net sample with vb.net backend sample is there one? Jake ASP .Net 0 02-09-2006 10:44 PM
Sample perl code does not work (from Perl cookbook, 2nd ed.) Staale Perl Misc 16 09-30-2004 12:17 PM
Error Running aspx file with IE ---> Expected ';' liam ASP .Net 1 05-24-2004 01:06 PM
PN: Sample code to call another program/command from inside a C++ program Peter Nolan C++ 4 04-08-2004 08:33 PM



Advertisments