Re: How to split a string read from the file?
- From: daggerquill@xxxxxxxxx (Jay Savage)
- Date: Wed, 29 Mar 2006 09:34:17 -0500
On 3/29/06, 庞 <pangzhy@xxxxxxxxx> wrote:
dear all , I'am trying to write a script to neaten files as follows:
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
Which output is 3 lines? 'print @temp?' or does $sum == 3?. There are
a number of things that could be happening here. First, print uses a
space, not a newline, to separate the elements of @temp when they're
printed to screen. You may have 17 elements, but that doesn't mean
you'll have 17 lines. Try
print join "\n", @temp;
Another issue is line breaks. If you're converting from Windows to
Unix, or vice versa, Perl may not be seeing your line breaks. Make
sure your data is correctly formatted for the OS you're using.
Finally, are you sure that every line of your input matches your
pattern? If not, some elements of @temp will be undefined.
Finally, Perl has a number of techniques to make this process much
simpler. look at the perldocs for 'foreach', 'push' and array slices
for starters:
foreach (@array) {
push @temp, (split /:/, $_)[1];
}
HTH,
-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential
daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org
values of β will give rise to dom!
- Follow-Ups:
- Re: How to split a string read from the file?
- From: pangzhy
- Re: How to split a string read from the file?
- References:
- How to split a string read from the file?
- From: pangzhy
- How to split a string read from the file?
- Prev by Date: RE: Whimsical Question
- Next by Date: SOAP::Lite perl client and .net webservice method.
- Previous by thread: Re: How to split a string read from the file?
- Next by thread: Re: How to split a string read from the file?
- Index(es):
Relevant Pages
|