develooper Front page | perl.perl5.porters | Postings from January 2001

Re: [ID 20010128.001] Problem with list ...

Thread Previous
From:
Philip Newton
Date:
January 28, 2001 21:47
Subject:
Re: [ID 20010128.001] Problem with list ...
Message ID:
200101290546.GAA84637@smtp2.nikoma.de
On 28 Jan 01, at 19:53, u99103@cs.unipune.ernet.in wrote:

> I want to break a long string in "file" to give me "file2" as output. 
> For this, I tried following code:
> 
> ### Start
> open(fd,"file");
> open(fd2,">file2");
> @array = <fd>;
> $i = 0;
> for ($i*70; $i < @array/70; @i++)
> {
> 	@temp = @array[$i..$i+70];
> 	print fd2 "@temp";
> }
> ### End
> 
> To my surprise simply 
> 
> print "@array[1..10]";
> OR 
> print "$array[1..10]";
> 
> were giving me the whole file NOT only 10 characters.
> 
> 
> Whether I am doing wrong or a BUG in perl?

You appear to be misunderstanding some things about Perl.

I'll go through your code:

> ### Start
> open(fd,"file");
> open(fd2,">file2");
> @array = <fd>;

This reads in the contents of "file", putting each line into a separate 
array element of @array. Lines will still contain their endings 
(probably "\n").

> $i = 0;

Sets $i to 0.

> for ($i*70; $i < @array/70; @i++)

This does not compile for me at all. It gives me the warning "Can't 
modify array deref in postincrement at -e line 1, near "@i++"". Is this 
really the code you used?

It calculates $i * 70 (which is 0 * 70 = 0, since you just set $i to 0) 
and throws this result away. Then it continues while $i is less than 
the number of lines in your file, divided by 70 (for example, if your 
file had 50 lines, this would be true for $i == 0 but not for $i == 1). 
So this is an infinite loop since you're not modifying $i anywhere.

> {
> 	@temp = @array[$i..$i+70];

This assigns elements from $i to $i+70 to @temp. If $i is 0, then @temp 
receives the first 71 lines from file, from $array[0] to $array[70]. If 
$i is 0, then @temp receives $array[1] to $array[71].

> 	print fd2 "@temp";

And this prints the lines in @temp to file "file2" separated by $", 
which is usually a space. The result will be that all lines after the 
first will begin with a space.

> }
> ### End

> To my surprise simply 
> 
> print "@array[1..10]";

This prints array elements from 1 to 10 (10 elements total), separated 
by $" (usually a space).

> OR 
> print "$array[1..10]";

Since "1..10" is in scalar context here, it's the "flip-flop" operator, 
not the "range" operator, which is almost certainly not what you want 
here. (`perldoc perlop`, section "Range Operators", for more 
information on the flip-flop and range operators, which are both 
spelled ".." (or "...").)

> were giving me the whole file NOT only 10 characters.

If you read from the file with @array = <fd>, then each element of 
@array is one line -- *not* one character. So @array[1..10] gives you 
the second to eleventh lines of the file.

If you want to have characters, you probably want something like 
"substr" (or possibly "split //"). See `perldoc -f substr` and `perldoc 
-f split` for more information on these functions.

If you can describe in more detail what it is exactly you are trying to 
do, the answer can be more specific. However, I suggest you ask the 
question on the newsgroup comp.lang.perl.misc, not with perlbug or to 
perl5porters.

Cheers,
Philip
-- 
Philip Newton <Philip.Newton@gmx.net>

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About