in reply to Re^2: Writing to a file using Formats
in thread Writing to a file using Formats

Yes, I would say the use of "warnings" and "strict" is THE most important thing here.

I hope that you also are able to use the standard command line loop I gave you to great effect during the coming years. This is a standard pattern that you will use over and over again.

When you write a "while" loop, you want the main termination condition to be right there in the while()! Not buried further on down in the guts. That's not to say that other exit conditions can't exist in the "body", just that the "normal" situation should be apparent.

The technique that I used is called the "comma operator". I guess crudely put, this is way to cram 2 or more statements into the place where normally just one would go. The truth of the while() is determined by the last comma statement, in this case whether this is a "quit" command or not.

Now, some folks go "crazy" with this comma thing. I mean you could pretty much cram 10 statements in there. That is a bad idea!

Yes, there are many fancy Perl modules for formatting text. I think that once you really understand printf() you will use either it or one of those modules and this format statement will fall by the wayside - or that's what happened in my learning experience. The format thing just didn't work out to give me as much control as I often needed.

Replies are listed 'Best First'.
Re^4: Writing to a file using Formats
by biohisham (Priest) on Aug 04, 2009 at 22:25 UTC
    In particular, I enjoyed the idea in the while() loop, many a times I used to end up on infinite loops but when I saw the way you did the while() and I remembered I saw that concept before but did not employ it, it looked convoluted, Now it looks precise, neat and sharp and I started doing it in my exercises.

    I used the printf() and sprintf() but not so exclusively, I mean, well, they are easier of course, in formatting you have the placeholders and alignment left, right, center and for numbers you have a different placeholder ####, now this can be frustrating if you did not know what that is or why your numbers don't show and you can't realize you had to use @####.##### instead of @<<<<, ask me about it :P

    I got stuck at formatting earlier and for that reason I abandoned learning Perl for two years and imagine coming back again and at formatting I did not wanna give up so I summoned an extra vigor. Practice Makes Perfect, I would Practice and I am cherishing all the replies from the Monks in here to get back to them every other while and see where I stand.

    thanks again and best of luck

    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind
      Great to hear that you are having fun the code!!

      One thing that I strongly suggest is "incremental development". Perl compiles and runs so quickly that it is possible to write some code and test it, then write more code. For example, to test the command loop, this is it:

      #!usr/bin/perl -w use strict; while ( (print "Enter a pair of numbers (space between) or quit: "), (my $line =<STDIN>) !~ /^\s*q(uit)?\s*$/i ) { print $line; }
      I'm not saying to try a whole bunch of random stuff in the hope that it will work, but rather take a small piece, think about the errors, make corrections, get that piece working then get the next piece working.

      Printing:
      I you are having trouble with a printf(), just use the standard Perl print, print "$var1 $var2\n"; to make sure that the variables are defined, etc. Use of "warnings" will show at runtime if a $var is undefined in the print.

      One common error in printf format statements is not leaving a space after each format descriptor if you want tabular output. The WIDTH is a minimum width, and if what is required is more, it will "spill over". Putting a space between format fields guarantees on whitespace character between columns which is usually what you want.

      #!usr/bin/perl -w use strict; my $string_a="ABCDEGFH"; my $string_b="X"; printf("%-5s%s\n", $string_a, $string_b); #$string_a is too long printf("%-5s %s\n", $string_a, $string_b); #space between fields printf("%-5s%s\n", $string_b, $string_a); #looks ok, but not! #If $string_b>=5 chars! #no space between columns. __END__ prints: ABCDEGFHX ABCDEGFH X X ABCDEGFH
        I think this spilling-over is an advantage in printf, because in formats if you are not setting the placeholders correctly, it trims the ends, for example
        my $string = "something"; format STDOUT= @<<<< $string . write; #"somet" and the rest is chopped away.
        and in my case, I go back and start counting the "<"s and recalculate, and imagine if someone uses binoculars for glasses and how their counting would be. So in printf all the intervention you got to do in case something is spilling over is to just add a space or anything, I mean, a little intervention...

        Frankly, , your tips are so amazing, I mean, they come to me at time that I need them emphasized over and over, usually, I do it this way, "incremental development" all the time, it takes time from me but I think I am gaining momentum, sometimes I lose track and go random, get stuck and come to PM to wench me out of the mud and other times I am smooth...hehehe

        ummm, finally, check this Personality Splits and Programming, ELISHEVA wrote some informative advice, you might wanna add yours too for us all to learn from.... thanks Marshall and I am indebted.
        Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind