Chern Jian Leaw wrote:
> 
> HI,

Hello,

> I would like to change output format of a file from:
> abinabdu adanie2  agibson  agoh1    aiabouse
> akko     alau     alee1    alee2    amitb
> amohdali amshams  anmohand
> 
> to the format listed below when printing it out to the standard output:
> 
> abinabdu
> adanie2
> agibson
> agoh1
> aiabouse
> akko
> alau
> alee1
> alee2
> amitb
> amohdali
> amshams
> anmohand
> 
> Each elements in the file are sepated by 1 or more spaces (see attached file).

perl -lane'print for @F' PSCS-ORIG.txt


> The script written below attempts to do so:
> #!/usr/bin/perl

use warnings;
use strict;

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

use Cwd;
my $currDir = cwd;

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

my $file = "$currDir/PSCS-ORIG";

> open(INFILE, "$file") or die("Cannot open $file : $!");
               ^     ^
Useless use of quotes.

open(INFILE, $file) or die("Cannot open $file : $!");

> @entries = <INFILE>;
> print "[EMAIL PROTECTED] ... \n";
> print "@entries ";
> close(INFILE);
> print "Printing contents ... \n";
> for($i=0; $i<@entries; $i++){
>    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]);

There is nothing for chomp() to do here.

>       print "$lineItems[$j] \n";
>    }
> }
> 
> 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
> @entries ...
> abinabdu adanie2  agibson  agoh1    aiabouse
> akko     alau     alee1    alee2    amitb
> amohdali amshams  anmohand

According to your code that should have looked more like:

@entries ...
abinabdu adanie2  agibson  agoh1    aiabouse
 akko     alau     alee1    alee2    amitb
 amohdali amshams  anmohand


>  Printing contents ...
> abinabdu adanie2  agibson  agoh1    aiabouse aicole
                                               ^^^^^^
Where did that last item come from?  I can not find it in the input
file.


> @lineItems = abinabdu adanie2 agibson agoh1 aiabouse
> 
> Printing @lineItems
> abinabdu
> adanie2
> agibson
> agoh1
> aiabouse
> 
> akko     alau     alee1    alee2    amitb
> @lineItems = akko alau alee1 alee2 amitb
> 
> Printing @lineItems
> akko
> alau
> alee1
> alee2
> amitb
> 
> amohdali amshams  anmohand
> @lineItems = amohdali amshams  anmohand
> 
> Printing @lineItems
> amohdali
> amshams
> anmohand
> 
> May I know where did I go wrong and how should I solve this problem?

It looks like you are getting the output the way you described it at the
beginning.  What exactly is not working the way you expected?  Did you
intend to put all the elements into a single array?

#!/usr/bin/perl
use warnings;
use strict;

my $file = 'PSCS-ORIG';
open INFILE, $file or die "Cannot open $file: $!";

my @entries = map "$_\n", map split, <INFILE>;
print @entries;

close INFILE;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to