Brett Williams wrote:
Hi :)

Ive been working all afternoon trying to get what seems like a fairly
simple outcome with split, but depite trying again & again i can't get
things going. I would very much appreciate any help/insight offered.
What im trying to do is read from a text file and use split then print
to display certain text within the file. Im aware of regular
expressions now, but find them too confusing at this stage (im an
extreme newbie to any programming).
The text file reads,

:Weekly expenses...:
:1002.00:
:125.00:
:61864.35:
:890876.99:
:9.99:

%Member number%
%3054%
%3567%
%3576%
%7845%

I want to print to screen all text between ":" on a new line. I
thought this would be easy :)
The final code i came up with is,

open (INPUT, "file.txt") or die "Error, can't find file\n";
$line = readline(INPUT);
while($line)
{
chomp($line);
@pricelist = split (/\:/, $line);
print @pricelist;
$line = readline(INPUT);
}

For some reason this code prints out the entire contents of the text
file (and fails to display every entry on a new line).

That is because (unlike other programming languages) you have to explicitly print the newline character ("\n").


BTW a while loop is usually written like:

while ( my $line = <INPUT> ) {
    chomp($line);
    @pricelist = split (/\:/, $line);
    print @pricelist;
    }



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