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

Discussion in 'Perl Misc' started by hiabhijeet@gmail.com, Dec 24, 2012.

  1. Guest

    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
    , Dec 24, 2012
    #1
    1. Advertising

  2. In article <>,
    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]
    Michael Vilain, Dec 24, 2012
    #2
    1. Advertising

  3. Guest

    On Monday, 24 December 2012 12:08:33 UTC+8, Michael Vilain wrote:
    > In article <>, 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.
    , Dec 24, 2012
    #3
  4. Guest

    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.
    , Dec 24, 2012
    #4
  5. Guest

    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!!
    , Dec 24, 2012
    #5
  6. Michael Vilain <> writes:
    > 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.
    Rainer Weikusat, Dec 24, 2012
    #6
  7. MeV Guest

    On Monday, December 24, 2012 11:50:50 AM UTC-8, Rainer Weikusat wrote:
    > Michael Vilain <> writes:
    >
    > > 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.
    MeV, Dec 25, 2012
    #7
  8. MeV <> writes:
    > On Monday, December 24, 2012 11:50:50 AM UTC-8, Rainer Weikusat wrote:
    >> Michael Vilain <> writes:
    >>
    >> > 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 ...
    Rainer Weikusat, Dec 25, 2012
    #8
  9. In article <>,
    Rainer Weikusat <> wrote:

    > MeV <> writes:
    > > On Monday, December 24, 2012 11:50:50 AM UTC-8, Rainer Weikusat wrote:
    > >> Michael Vilain <> writes:
    > >>
    > >> > 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]
    Michael Vilain, Dec 25, 2012
    #9
  10. Michael Vilain <> writes:
    > In article <>,
    > Rainer Weikusat <> wrote:
    >> MeV <> 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.
    Rainer Weikusat, Dec 25, 2012
    #10
  11. Re: Sample program from Programming Perl (Camel book) not runningas expected

    On 25/12/2012 01:09, MeV wrote:
    > On Monday, December 24, 2012 11:50:50 AM UTC-8, Rainer Weikusat
    > wrote:
    >> Michael Vilain <> writes:
    >>
    >>> 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.
    >
    > 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.


    Notepad++ will let you change line endings on Windows, for what it's worth.

    - S
    Stuart Golodetz, Jan 3, 2013
    #11
  12. Andrew Guest

    >> >>
    >
    > >> >> > 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.


    I simply strip the carriage returns ("\t") whenever I get files of this type.

    You can write a Perl script to batch-process your files, to simply strip the "\r", or you can use the command-line approach:

    perl -pi.bak -e 's/\r//g' *.pl
    Andrew, Jan 5, 2013
    #12
  13. Andrew Guest


    > I simply strip the carriage returns ("\t") whenever I get files of this type.


    oops. Bad typo. I meant to write "\r", in both cases. Sorry.
    Andrew, Jan 5, 2013
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Petrou

    Q: good time to buy the camel book?

    David Petrou, Oct 13, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    124
    David Oswald
    Oct 13, 2003
  2. Suresh Govindachar

    Camel book: why invoke in the manner @{[&whowasi]}?

    Suresh Govindachar, Aug 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    144
    Brian McCauley
    Aug 12, 2004
  3. Vijai Kalyan
    Replies:
    2
    Views:
    159
    Vijai Kalyan
    Oct 17, 2004
  4. Bernhard Walle

    [OT] Perl Code which draws camel

    Bernhard Walle, Feb 1, 2005, in forum: Perl Misc
    Replies:
    7
    Views:
    295
  5. rihad
    Replies:
    7
    Views:
    114
    comp.llang.perl.moderated
    Oct 29, 2007
Loading...

Share This Page