[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Code cleaning and questions





Ronald J Kimball wrote:

> On Thu, Jun 24, 1999 at 09:48:04AM -0500, Robert Pollard wrote:
> > Hello everyone,
> >
> > I have a snippet of code I would like to ask questions about.  I am a
> > newbie so I won't even raise your brainwave level far above a coma.
> > I did this little bit of code to check the contents of a file for any
> > strange characters.  The file was causing a freeze and I thought there
> > may be something odd in it.  It turned out there wasn't but I have some
> > questions about it.
> >
> > open(SOURCE, '<Robert:Desktop Folder:Addresses') or die;
> > open(DEST, '>Robert:Desktop Folder:Addresses.clean') or die;
> > 1)   Does the open command always read the whole file in.
>
> The open() function does not read any of the file in.  It just creates a
> filehandle that you can use to read the file in later.  (Or write to, or
> whatever, as appropriate.)
>
> > This is an
> > interesting syntax because what this did was to put the whole contents
> > of a file line by line into an array.  What if my text were very long
> > paragraphs and exceeded the maximum character limit of the variable for
> > each element?
> >    @line = <SOURCE>;
>
> I'm not sure what would happen if you had a line that was longer than the
> maximum character limit.  I believe that limit is somewhere between 255
> megabytes and 4095 megabytes for a single scalar value, at least on the

This is very good

>
> system I did a bit of testing on just now - at the upper end, the scalar
> came back empty.  Anyway, it's more likely MacPerl will run out of memory
> before you manage to fill up a scalar variable.
>
> > 2)  Why do I need to chomp.  The program ran fine and put everything
> > aout like it was suppose to.  I believe this is for stripping the CRs at
> > the end of the line?
> >    #chomp($line);
>
> Yes, that is what it is for.  If you're not _sure_ what chomp() does, why
> didn't you read the documentation???

Because I couldn't get to the documentation to double check what I thought I
had read.  I have the book on order for over 2 weeks with no indicator of when
I will see it and the online version wasn't available.  I believe the server
was down.  I checked this morning and it still wasn't available.  I apologize
for not being able to remember things that I glance at, but there is so much
to take in.

>
>
> If you don't want to remove the newlines, you don't need to chomp().
>
> > print "@line\n";
>
> Are you sure this is what you want to do?  This prints with a space between
> each array element, and adds an extra newline at the end.  You probably
> meant:
> print @line;
>
> Read the documentation on quote operators and the $" variable.
>
> > foreach $line (@line) {
> >    ($Name, $Phone, $Address, $Type) = split(/\t/, $line);  # This cool.
> > I would use this before I would allow the split to assign each line
> > # to different elements in the array.  In the importation of records,
> > this seems to be the best way to write to arrays in row/column fashion.
> >
> > 3)   Here I didn't have to use the angle brackets around the
> > filehandle.  If I did it seemed to refer to the whole file.  Without
> > them, it allows whatever you want to put out.  As in this case I was
> > able to put out a whole line at a time.
> >    print DEST $line;
>
> Angle brackets around a filehandle are the _input_ operator.  If you're
> trying to output to a filehandle, you should not use the input operator on
> it.  Once again, why haven't you read the documentation???

I'm sorry but this tidbit didn't jump out at me when I went over the
documentation.  I went back and looked for it under "Builtin functions" under
"open" and still didn't see this.  Maybe they assume you can figure it out.

>
>
> >    print("The name is $Name.\n");
> > # This is where I convert each character to the ascii representation.
> >    print("The ascii representation is:\n");
> >    for ($i=1;$i <= length($line);$i++) {
> >       $num = ord(substr($line, $i, 1));
> >       print("$num,");
> >       if ($num > 127) {
> >         print ("\n\nW A R N I N I N G -- ASCII value exceeds 127\n\n");
> >       }
> >    }
> >    print("\n");
> > }
> > close(SOURCE);
> > close(DEST);
> > 4)   Could you guys give me any tips/hints on code optimization?
> >
>
> Tip #1.  Read the documentation.

I have and am continuing to.  You may be one of those genius types that can do
things like look at hexadecimal code and know exactly what it does.  Me, it
takes time.

>
>
> Tip #2.  Try to program in Perl, not in C.  The for loop above is how you
> would solve this problem in C.  A more Perlish way would be:
>
> foreach (split //, $line) {
>   print $num = ord $_;

I have looked for "$_" throughout the documentation in both the MacPerl: Power
and Ease and in the documentation.  I can't find it anywhere.  I wish I could
do a keyword search but I can't and it takes time to find these things.

Thanks for the Perlish way of doing this.  I am having a hard time adjusting
to Perl.  This is what I was hoping someone would show me.  I hope you
remember in the first part of this request I said I was a newbie.  If I had as
much time in Perl development as you I would understand your frustration in my
basic level questions.  Everybody has to start somewhere.  I have been
programming in 4th Dimension, SQL and a little C for so long, Perl is really
strange to me.
I can only offer you 2 options: 1) Please be patient with me or 2) if I annoy
you then don't respond to any of my requests.

Still and yet, thank you for taking the time.

Robert Pollard

>
>   $num > 127 and warn "WARNING -- ASCII value exceeds 127.\n";
> }
>
> Ronald


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org