Basic array output problem

Basic array output problem

am 21.10.2006 02:51:38 von Paul_B

Hi. I'm trying to rearrange elements in an array, but I'm getting
truncated results.

I've got a data file organized as:

field1, field2, field3, field4

Here's the script:

open (IN , 'C:\...\test2.csv');

while() {

@array = split(/(,)/, $_);
print;

print $array[0];
print $array[1];
print $array[2];
print $array[3]; print "\n";
}

And here's the form of the output:

field1, field2, field3, field4
field1, field2,

Why aren't the last two fields being printed?

Thanks,
p.

Re: Basic array output problem

am 21.10.2006 03:43:22 von Gunnar Hjalmarsson

Paul_B wrote:
> I've got a data file organized as:
>
> field1, field2, field3, field4
>
> Here's the script:
>
> open (IN , 'C:\...\test2.csv');
>
> while() {
>
> @array = split(/(,)/, $_);
> print;
>
> print $array[0];
> print $array[1];
> print $array[2];
> print $array[3]; print "\n";
> }
>
> And here's the form of the output:
>
> field1, field2, field3, field4
> field1, field2,
>
> Why aren't the last two fields being printed?

Because you don't tell Perl to print them.

Please read about the split() function in "perldoc perlfunc", and note
the remark about parentheses in the PATTERN.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Basic array output problem

am 21.10.2006 04:17:18 von Paul_B

On Sat, 21 Oct 2006 03:43:22 +0200, Gunnar Hjalmarsson wrote:

> Paul_B wrote:
>> I've got a data file organized as:
>>
>> field1, field2, field3, field4
>>
>> Here's the script:
>>
>> open (IN , 'C:\...\test2.csv');
>>
>> while() {
>>
>> @array = split(/(,)/, $_);
>> print;
>>
>> print $array[0];
>> print $array[1];
>> print $array[2];
>> print $array[3]; print "\n";
>> }
>>
>> And here's the form of the output:
>>
>> field1, field2, field3, field4
>> field1, field2,
>>
>> Why aren't the last two fields being printed?
>
> Because you don't tell Perl to print them.
>
> Please read about the split() function in "perldoc perlfunc", and note
> the remark about parentheses in the PATTERN.


Excellent. Thanks much for your help, Gunnar.

Paul