Re: Changing file output format

From: R. Joseph Newton (rjnewton_at_efn.org)
Date: 02/28/04


Date: Sat, 28 Feb 2004 14:25:47 -0800
To: "Leaw, Chern Jian" <chern.jian.leaw@intel.com>


"Leaw, Chern Jian" wrote:

> HI,
> I would like to change output format of a file from:
> ...

> to the format listed below when printing it out to the standard output:
>
> abinabdu
> ...
> Each elements in the file are sepated by 1 or more spaces (see attached file).
>
> The script written below attempts to do so:

...and what part of the code is attempting to make that change? What is your overall strategy with this code?

>
> #!/usr/bin/perl

use strict; # always
use warnings; # almost always

>
> $currDir = `pwd`;
> chomp($currDir);

Don't use chomp here. The chomp function is for situations where you may not know whether a line is terminated by a newline. You just assigned a value to pwd, so it
is pointless to then chomp it.

> $file = $currDir."/PSCS-ORIG";

would be much more readable [and understandable] as
$file = $currDir . "/PSCS-ORIG";

> open(INFILE, "$file") or die("Cannot open $file : $!");
> @entries = <INFILE>;

This loaded the first element of @entries with the first line of the file. The rest of your program works on this single element array.
This is a lot of busy work. If you are not manipulating the data set as a whole, there is no good reason to put it into an array. Just read the lines, split them on
whitespace, and output them.

while (my $mutli_item_line = <INFILE>) {
   chomp $mutli_item_line;
   $mutli_item_line =~ s/^\s+//;
   my @items = split /\s+/, $mutli_item_line;
   print "$_\n" foreach @items;
}

>
> print "\@entries ... \n";
> print "@entries ";

Please don't make a habit of printing arrays as a whole. Perl does allow you to do this, in some contexts, but usually this is much less useful than dealing with the
data as you iterate through it.

>
> close(INFILE);
> print "Printing contents ... \n";
> for($i=0; $i<@entries; $i++){

You don't need to do this in Perl. You are not using the indexes, except to get the elements, so you would be better off using a
foreach $line (@entries) # why @entries? Are these really "entries"?

>
> chomp($entries[$i]);
> print "$entries[$i] \n";
> @lineItems = split(/\s+/, $entries[$i]);
> print "\@lineItems = @lineItems\n";
> print "\n";
> print "Printing \@lineItems \n";
> for($j=0;$j<@lineItems;$j++){
> chomp($lineItems[$j]);
> print "$lineItems[$j] \n";
> }

The for loop above does e4xactly what you want. Unfortunately, it does it only on the single line the program has read.

>
> }
>
> However, the script did not print the entire array elements all at once, and that the elements are not joined together in the array, as shown in the output below:
>
> pglc0002> ./extract.pl
>

...

> anmohand
>
> May I know where did I go wrong

I think you just got too busy, with not enough attention to understanding what each line was doing. Make sure you know why you are doing each thing you do.

> and how should I solve this problem?

Think for yourself. Sample code and models are meant to be used to understand the principles by which the function. Then we write programs by applying these
principles. I see indicatons here that you are applying code from samples out of context. You can not program successfully that way.

For each line of code that you write, I would recommend that you say out loud [so other folks will look at you wierd. Big deal] what you expect that line to
accomplish, and how the identifiers and operator in that line will achieve the desired effect. Listen to yourself and see if it makes sense.

I saw code in the script above that did achieve the desired effect on the data that was provided, but I really can't tell whether you understood what it was doing
when you wrote it. I think it is entirely possible that you could fix the whole thing by adding parentheses in one place:
my @entries = (<INFILE>);
but I almost hate to do that, because I'd rather see you dispense with this heavy code, and write some more efficient code that does just what you need--no more and
no less.

Joseph



Relevant Pages

  • Re: array within array
    ... I'm new with Perl and need help with this simple script. ... below is incomplete and I'm doing an array within an array which is ...
    (perl.beginners)
  • Emptying Arrays
    ... I'm working on a script that iterates over a log file looking for known ... hosts so that their messages can be grouped in a summary report. ... I thought that I could empty the array ... # chomp the array ...
    (perl.beginners)
  • Re: string retrieval issue
    ... Chicago Bears|NFC North ... not writing the third element back to the array). ... You didn't include it in your script. ... Fear is the mind-killer. ...
    (comp.lang.perl.misc)
  • Re: even rows for checkbox forms with no splitting of boxes from values
    ... 'mod' on the array length with a denominator equal to the width of the row, ... State saving really depends on what the script is already doing. ... So I usually roll my own checkboxes. ... #this is to create an array with 25-50 strings 2-10 in length ...
    (comp.infosystems.www.authoring.cgi)
  • Re: settimeout needs alert() ???
    ... function slider { ... and use script to replace the src and title attributes. ... are downloaded completely. ... The usual strategy is to load all of the images in to an array of image ...
    (comp.lang.javascript)