loop362 has asked for the wisdom of the Perl Monks concerning the following question:

Aloha Monks,

I am extremly new to perl. My second script. I have written a script to collect four pieces of data and write it to a file. I have then been able to open the file and read the data as an array.

What I would like to know is there a more fexible way in calling the data, so it can be manipulated in layout. I was thinking $variable($count) with a for loop with the size of the array/4 or am I way off base?

I have used strict, and still don't realize what it does, but many people think it is important habit to get into so I'm trying.

Also newline \n doesn't work! Mac OS X Apache BBEdit Adobe GoLive.

Mahalo JW Guillaume

#!/usr/bin/perl -Tw use strict; use CGI qw(:standard); print "Content-Type: text/html\n\n"; ##################################### my $employee=param('employee_name'); my $i=""; my $t=""; my @data=""; my $items; my $lines; ##################################### # Read File # ##################################### print "Employee's name is: $employee<p\/>"; open(JOE, "<Files/$employee.txt"); @data = <JOE>; close (JOE); ##################################### # Print File # ##################################### $lines=scalar@data/4; for ($t=1; $t<=$lines; $t++) { for ($i=1; $i<=4; $i++) { $items=shift(@data); print $items; } print "there is a line break here<p\/>"; } ##################################### print "<p\/>To go Back <A HREF=\"http://127.0.0.1/\">Click here</A>"; ##################################### exit;

20050518 Janitored by Corion: Put code in code tags

Replies are listed 'Best First'.
Re: Array manipulation
by davido (Cardinal) on May 18, 2005 at 21:49 UTC

    \n newline doesn't work in your situation because HTML-rendering browsers don't respect newline characters, except probably within <pre></pre> tags. If you want a new line in general HTML documents, you have to use a HTML tag or set of tags. For example, wrap the text in <p>...</p> tags, or for a line break use <br />.

    If you're trying to get tabular data to render in HTML form, your script needs to output the proper HTML to do that too. More often than not, that means using tables. You do that by outputting <table></table><tr></tr><td></td> and so on.. tags, in accordance with proper HTML formatting practices.

    Update for clarification:
    This actually has a lot less to do with array manipulation than it does with understanding the interplay between a Perl based CGI script, and the client's browser. Think of what you would write in a static HTML web page, and then understand that Perl must output that. If you would write a web page like this:

    <head> <title>My page</title> <h1>My page</h1> </head> <body> <p>Hello world!</p> </body>

    ...then you need your Perl script to output all that, tags and all. If you want the browser to render data in table format, output the html tags for a table, intersperced with the actual data.

    Update 2: Sorry, I keep thinking of more to say.
    Regarding the use of strictures (use strict;): Strict is basically a tool that helps you to make sure you're not accidentally breaking tried and proven rules of conduct. With strictures enabled you can't, for example, accidentally stumble into the trap of using symbolic references. A misspelled variable or function name will become more easily apparent. And it encourages you to learn to use lexical scoping, or at very least to declare your globals. With time you'll learn more about how it helps you to live a clean and healthy life.

    Welcome to the Monastery!


    Dave

Re: Array manipulation
by scmason (Monk) on May 18, 2005 at 22:23 UTC
    I want to give more specifics on the use of strict and how it can make your life easier. Consider the following code:
    $max_count = 100; $counter = 0; while( $counter < $max_count ){ $conter++; print "COUNT = $count" }

    You would expect to see:

    COUNT = 0 COUNT = 1 ... COUNT = 99

    Instead you would see:

    COUNT = 0 COUNT = 0 COUNT = 0 ...
    And so on until you interupted the program. If this was from a more complex code sample, it may not be obvious at first that you were never incrementing the variable named "counter", but instead another variable named "conter".

    With strict turned on, the code would look like this:

    my $max_count = 100; my $counter = 0; while( $counter < $max_count ){ $conter++; print "COUNT = $count" }

    The compilation would fail and you would be informed that there was no variable declared named 'conter'.

    The reason for this is that Perl does not have strongly typed variables, which can lead to problems that only manifest themselves at runtime. Using strict is a way to get around some of these problems.

Re: Array manipulation
by TheStudent (Scribe) on May 19, 2005 at 00:24 UTC

    Since you claim to be new (like me), some comments on your code:

    my $i=""; my $t=""; my @data="";

    Variables that have never been used have the special undef value. What happens if you use a variable that has the undef value? Nothing serious

    If you use it as a number it takes on the value 0 (zero). If you use it as a string it takes on the value of an empty string. You do not have to set them to "". You use $i and $t as numeric values in your code so setting them to "" is kinda confusing.

    Setting the array @data to "" does not make much sense for an array either. If you want to empty an array assign it to an empty list:

    my @data = ();

    But a new array starts out this way anyway.

    Now lets take a look at your loops...

    $lines=scalar@data/4; for ($t=1; $t<=$lines; $t++) { for ($i=1; $i<=4; $i++) { $items=shift(@data); print $items; }

    ($t=1; $t<=$lines; $t++) works, but it is more common to start with $t=0 and use $t<$lines as the termination.

    Getting used to this will prevent you from getting into trouble when indexing through arrays which start with an index of zero. It also prevents a finger-flub of forgetting the = in the condition.


    Lou

    Update: Oh, one more thing that is good to get used to when you are new. Besides using use strict;, also consider...

    use warnings; # flags many questionable constructs use diagnostics; # makes error messages much more understandable

    Lou

      A minor point on your update.

      The OP's code starts with:

      #!/usr/bin/perl -Tw
      So warnings are on. Taint mode is also set which will also contribute to davido's "clean and healthy life".

      Note: I am of course assuming that the w wasn't added after you posting.

      Thank you for advice Lou.
      BBEdit creates a sytax error if I don't declare the variables as empty, that is why I do it that way.
      JWG
      Good points, but I would like to make an argument why it is a good practice to intiialize variables by hand.

      Readability - traceability is important. Since Perl is not a strongly typed language, it is often more difficult to understand the authors intentions on a given variable. In C/C++/Java, you know what a variable will be used for not only by its name, but also by its type.

      By initializing a variable to the empty string or 0, you let the reader know more about your intended purposes with the variable.

      "Never take yourself too seriously, because everyone knows that fat birds dont fly" -FLC