in reply to matrices and non-numerical values

Just two things I can be bothered commenting on:

Prints:

  1. while (chomp($line = <IN>)) will not work as you expect. Read the chomp documentation
  2. use sensible identifiers and at least fix the strictures errors before you post.
True laziness is hard work

Replies are listed 'Best First'.
Re^2: matrices and non-numerical values
by thschhk (Initiate) on Apr 30, 2012 at 01:31 UTC
    use strict; use warnings; open(IN, "list.txt") or die; chomp($line = <IN>); ($element, @abc) = split /\s+/, $line; while (chomp($line = <IN>)) { ($values, @xyz) = split /\s+/, $line; for ($i=0; $i<@xyz; $i++) { $s{$values}{$abc[$i]} = $xyz[$i]; } } close(IN);

    I made the identifiers sensible, but I am not sure what to assign as a explicit package for i, lines 6-10. My understanding is that it should get that information from the file being invoked.

      Your code and logic is rather convoluted.

      I offer:

      use strict; use warnings; my %element; while (<DATA>){ my ($elem, $value) = split; $element{$elem}= $value; } print " "; print "$_ " for sort keys %element; print "\n"; for my $k1(sort keys %element){ print "$k1 "; for my $k2(sort keys %element){ my $val = $element{$k1} eq "X" ? 0 : $element{$k2} eq "X" ? 0 : 2; print $val . " "; } print "\n"; } __DATA__ A X B Y C Z D X E X

                   I hope life isn't a big joke, because I don't get it.
                         -SNL

      Can you please show the output you get when you run this code?

        I revised the code some more according to what makes sense. And I get errors regarding uninitialized values @values and @elements in hash element at each line of the file, list.txt.

        use strict; use warnings; open(IN, "list.txt") or die; chomp($line = <IN>); ($element, @abc) = split /\s+/, $line; while (chomp($line = <IN>)) { ($values, @xyz) = split /\s+/, $line; for ($i=0; $i<@xyz; $i++) { $s {$elements}{$values[$i]} = $xyz[$i]; } } close(IN);