"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 "[EMAIL PROTECTED] ... \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 "[EMAIL PROTECTED] = @lineItems\n"; > print "\n"; > print "Printing [EMAIL PROTECTED] \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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>