gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I know this should be simple, but I am tired and am not seeing the problem. Here's my code:

use strict; use warnings; use Data::Dumper; my @trainset = (); while (my $pattern = <DATA>) { chomp $pattern; next if ($pattern =~ /#/); my ($inputs,$outputs) = split(/=>/,$pattern); my @inputs = split(/,/,$inputs); my @outputs = split(/,/,$outputs); $trainset[scalar(@trainset)] = \@inputs; $trainset[scalar(@trainset)] = \@outputs; } print Dumper(\@trainset); my @set = ( [0,0] => [0], [0,1] => [1], ); print Dumper(\@set); __DATA__ 0,0=>0 0,1=>1 #not used

The two arrays should be the same, but the output of Data::Dumper on the two arrays is different (when read in from a handle, the values in the arrays are scalars instead of numbers) and I think it's causing me problems further on. What am I doing wrong?

Thanks a bundle.

Replies are listed 'Best First'.
Re: Array definition error
by sasikumar (Monk) on Apr 15, 2005 at 15:32 UTC
    Hi,
    If i had understood the question right. Perl has no clue in finding the datatype of ur @outputs and @inputs. Give it some clue to find it out. It would do what u were asking for.
    Try this
    use strict; use warnings; use Data::Dumper; my @trainset = (); while (my $pattern = <DATA>) { chomp $pattern; next if $pattern =~ /^\s*$/; next if ($pattern =~ /#/); my ($inputs,$outputs) = split(/=>/,$pattern); my @inputs = split(/,/,$inputs); my @outputs = split(/,/,$outputs); for(my $i=0;$i<@outputs;$i++) { $outputs[$i]=$outputs[$i]+1-1; } for(my $i=0;$i<@inputs;$i++) { $inputs[$i]=$inputs[$i]+1-1; } $trainset[scalar(@trainset)] = \@inputs; $trainset[scalar(@trainset)] = \@outputs; } print Dumper(\@trainset); my @set = ( [0,0] => [0], [0,1] => [1], ); print Dumper(\@set); __DATA__ 0,0=>0 0,1=>1 #not used
    Thanks
    SasiKumar
      That works... or...(if this is even a necessary step)
      use strict; use warnings; use Data::Dumper; my @trainset = (); while (my $pattern = <DATA>) { chomp $pattern; next if ($pattern =~ /#/); my ($inputs,$outputs) = split(/=>/,$pattern); my @inputs = split(/,/,$inputs); my @outputs = split(/,/,$outputs); $_+0 foreach @outputs; # added $_+0 foreach @inputs; # added $trainset[scalar(@trainset)] = \@inputs; $trainset[scalar(@trainset)] = \@outputs; } print Dumper(\@trainset); my @set = ( [0,0] => [0], [0,1] => [1], ); print Dumper(\@set); __DATA__ 0,0=>0 0,1=>1 #not used
      --------------
      "But what of all those sweet words you spoke in private?"
      "Oh that's just what we call pillow talk, baby, that's all."
Re: Array definition error
by RazorbladeBidet (Friar) on Apr 15, 2005 at 15:21 UTC
    What are you trying to do?
    $trainset[scalar(@trainset)] = \@inputs; $trainset[scalar(@trainset)] = \@outputs;
    Is going to set the value to be \@outputs only (it will override).

    So you should get 0 and 1 (the values of the outputs only) as your array elements.


    Ok, nevermind, I'm thoroughly confused :)

    I tested this and found the outputs to be the same except that the @trainset values are treated as strings.

    $VAR1 = [ [ '0', '0' ], [ '0' ], [ '0', '1' ], [ '1' ] ]; $VAR1 = [ [ 0, 0 ], [ 0 ], [ 0, 1 ], [ 1 ] ];
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
      Actually that's not true. The line $trainset[scalar(@trainset)] = \@inputs; will increase the side of the @trainset array so that when $trainset[scalar(@trainset)] = \@outputs; is called, scalar(@trainset) will be one larger and therefore not overwrite the original data. This is akin to a push.

        You statment is true. However, I'd like to ask why you are not simply using push? Does this method offer some benifit that I do not see? If so, please enlighten me.


        A truely compassionate attitude towards other does not change, even if they behave negatively or hurt you

        —His Holiness, The Dalai Lama

Re: Array definition error
by gri6507 (Deacon) on Apr 15, 2005 at 15:21 UTC
    I truly am tired. I found out what was causing my problems later on in my program. (The rest of my program uses compiles XS code and I was passing incorrect parameters to it).

    However, for purely academic reasons, my question still remains: how are the two arrays different?

      The one you're building has strings as its fundamental elements. The other has numbers.

      Caution: Contents may have been coded under pressure.
Re: Array definition error
by sh1tn (Priest) on Apr 15, 2005 at 15:22 UTC
    It's because of the empty lines:
    ... while (my $pattern = <DATA>) { next if $pattern =~ /^\s*$/; ...


      what empty lines? The __DATA__ only has three lines and they are all non-empty.