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



dear Shawn & Jay
problem solved by using '<' instead of 'lt'
but the $temp[2] is undefined
which means the pattern is not appropriate



在06-3-29,Jay Savage <daggerquill@xxxxxxxxx> 写道:

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!




--
Terry Pang


Relevant Pages

  • String 15 chars long; 56KB size
    ... I'm writing a parser which uses Pattern and Matcher from ... After about 300 parses i receive a outofmemory ... The temp variable is 56KB big but only 15 chars long. ...
    (comp.lang.java.programmer)
  • Re: How to split a string read from the file?
    ... print @temp; ... Unix, or vice versa, Perl may not be seeing your line breaks. ... look at the perldocs for 'foreach', 'push' and array slices ...
    (perl.beginners)
  • Re: CrossTabulate names
    ... A very nice and compact way to do it is with the following pattern: ... GROUP BY PKey ... other methods that use temp tables or correlated subqueries. ...
    (microsoft.public.sqlserver.datamining)
  • Re: Joined Row Update
    ... If you can't wait that long, an alternative is a Foreach loop in a ... -- lock table in exclusive mode is done so as not to run out of locks ... create temp table real_table ... Update table1 ...
    (comp.databases.informix)
  • Accessing Hash keys alphabetically
    ... foreach $temp ) ... sort of the keys. ... I am sure there is some perlish way of doing this with a ...
    (comp.lang.perl.misc)