Re: How to split a string read from the file?



dear John
that's what I want , thx


2006/3/30, John W. Krahn <krahnj@xxxxxxxxx>:

ÅÓ wrote:
dear all , I'am trying to write a script to neaten files as follows:

1 : #!/usr/local/bin/perl
2 :
3 : while ($inputline = <STDIN>) {
4 : while ($inputline =~ /\b[A-Z]\S+/g) {
5 : $word = $&;
6 : $word =~ s/[;.,:-]$//; # remove punctuation
7 : for ($count = 1; $count <= @wordlist;
8 : $count++) {
9 : $found = 0;
10: if ($wordlist[$count-1] eq $word) {
11: $found = 1;
12: $wordcount[$count-1] += 1;
13: last;


I want to clear the number and colon at the beginning of each line and
wirte
a perl script

#/usr/bin/perl -w
use strict ;

open MYFILE , "my.txt" ;
my @array=<MYFILE>;
my $count=0;
my @temp;
my $sum=0;

for($count=0;$count lt @array ; $count++){
(my $number,$temp[$sum])=split (/:/,$array[$count]);
print $number,"\n";
$sum++;
}

print @temp;


but the output just three lines instead of 17

You probably want something like:

#/usr/bin/perl -w
use strict ;

open MYFILE , 'my.txt' or die "Cannot open 'my.txt' $!";

my @temp;

while ( my $line = <MYFILE> ) {
( my $number, $line ) = split /:/, $line, 2;
print "$number\n";
push @temp, $line;
}

print @temp;

__END__


John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
<http://learn.perl.org/> <http://learn.perl.org/first-response>





--
Terry Pang


Relevant Pages

  • Re: Transform hash key?
    ... > I'd like to change a hash's keys from all lower cases to upper ... > The code below works but it just doesn't *feel* right with %temp. ... > use strict; ... > use warnings; ...
    (comp.lang.perl.misc)
  • Re: How to split a string read from the file?
    ... use strict; ... open MYFILE, "my.txt"; ... print @temp; ...
    (perl.beginners)
  • change char, keep case
    ... how you would write this without $temp: ... use strict; ... use warnings; ... but I want keep case of chars, ...
    (perl.beginners)
  • Re: problems with %temp%
    ... I did the set temp command and then tried ... After running that command %temp% or %Temp% both yielded: ... There is a colon missing between C and \Documents... ... from a forum on paint.net I was asked to check my temp files by using ...
    (microsoft.public.windowsxp.perform_maintain)