Basic array output problem

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(<IN>) {

[at] 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.
Paul_B [ Sa, 21 Oktober 2006 02:51 ] [ ID #1508810 ]

Re: Basic array output problem

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(<IN>) {
>
> [at] 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
Gunnar Hjalmarsson [ Sa, 21 Oktober 2006 03:43 ] [ ID #1508811 ]

Re: Basic array output problem

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(<IN>) {
>>
>> [at] 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
Paul_B [ Sa, 21 Oktober 2006 04:17 ] [ ID #1508812 ]
Perl » alt.perl » Basic array output problem

Vorheriges Thema: Regex, getting closer
Nächstes Thema: Re: IM ASKING FOR DONATIONS FOR HEART SURGERY