For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > How to split a string read from the file?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to split a string read from the file?
pangzhy@gmail.com

2006-03-29, 7:57 am

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


--
Terry Pang

Mr. Shawn H. Corey

2006-03-29, 6:57 pm

On Wed, 2006-29-03 at 20:44 +0800, 庞 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++){


for( $count = 0; $count < @array; $count ++ ){
# 'lt' is for strings, '<' is for numbers

> (my $number,$temp[$sum])=split (/:/,$array[$count]);
> print $number,"\n";
> $sum++;
> }
>
>
> print @temp;
>
>
> but the output just three lines instead of 17
>
>
> --
> Terry Pang

--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



usenet@DavidFilmer.com

2006-03-29, 6:58 pm

pangzhy@gmail.com wrote:
> I want to clear the number and colon at the beginning of each line


Why are you slurping the file into an array (instead of just iterating
over the source file)? Why are you using split instead of a regular
expression? Why are you using all those intermediate variables? Your
task can be made much easier:

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

while (<DATA> ) { #you would use a handle to your file here
s/^\d+\s*: ?// && print;
}

__DATA__
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;


--
http://DavidFilmer.com

John W. Krahn

2006-03-29, 6:58 pm

ÅÓ 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
pangzhy@gmail.com

2006-03-29, 9:57 pm

dear John
that's what I want , thx


2006/3/30, John W. Krahn <krahnj@telus.net>:
>
> ÅÓ wrote:
> wirte
>
> 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@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



--
Terry Pang

Gerard Robin

2006-03-30, 6:57 pm

I am off topic regarding your question about split, but with a one-liner li=
ke=20
this perl does the work for you:

perl -pi~ -e 's/^\d+ *: *//' file.txt

hth
--=20
G=E9rard

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com