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



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!


Relevant Pages

  • Re: Need ideas on how to make this code faster than a speeding turtle
    ... >> why are you calling out to a program when perl can load web pages just ... >> you have the text in $temp. ... you know how to use backticks but why do you ...
    (comp.lang.perl.misc)
  • Re: How to split a string read from the file?
    ... which means the pattern is not appropriate ... print @temp; ... look at the perldocs for 'foreach', 'push' and array slices ...
    (perl.beginners)
  • perl + script files from only one directory
    ... Does anyone know if it is possible to configure perl in such a ... totally wrap all its files and unpack and run them from a single exe, ... as long as all needed files are in the same directory... ... some real temp files being created in the temp directory, ...
    (comp.lang.perl.misc)
  • Re: Printing number of elements in an array
    ... print $temp ... There is no need to put a semicolon after a Perl statement. ... printon unopened filehandle 3 ... an unopened file handle. ...
    (comp.lang.perl.misc)
  • Perl/DBI/ORACLE Troubles. Help, please!
    ... Oracle 9.0.3 iAS. ... Through Perl, I can insert and select counts with no ... The account profile attempting to run the perl script contains the ... TEMP=~/tmp; export TEMP ...
    (perl.dbi.users)