in reply to Re: matrices and non-numerical values
in thread matrices and non-numerical values

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.

Replies are listed 'Best First'.
Re^3: matrices and non-numerical values
by NetWallah (Canon) on Apr 30, 2012 at 02:04 UTC
    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

Re^3: matrices and non-numerical values
by stevieb (Canon) on Apr 30, 2012 at 01:48 UTC

    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);

        You did not run the code you show us or you would have gotten messages that look like:

        lobal symbol "$line" requires explicit package name at noname.pl line +5. Global symbol "$element" requires explicit package name at noname.pl l +ine 6. ...

        Prefix your code with:

        use strict; use warnings; my $fname = 'list.txt'; open my $fOut, '>', $fname or die "Can't create $fname: $!\n"; print $fOut <<TESTDATA; some test data goes here TESTDATA close $fOut; ...

        providing some sensible test data where indicated then show us the result of actually running the code.

        True laziness is hard work