http://www.perlmonks.org?node_id=1006305


in reply to Re: Sorting array, getting modification of read-only value error
in thread Sorting array, getting modification of read-only value error

I fixed the ==, got rid of $x. @char is used somewhere else where it actually is an array so I borrowed the code from that and the $x stuck in there, and it wasn't causing a problem so I left it. I was trying to use $a=(); to make the sort work, in case I had defined $a and $b earlier and it was messing up the sort. I'll look up the other point you made in #4 to help with file opening. I know all this probably annoys you guys, but I'm still learning how to keep my code neat. syntax, then logic, then neatness I guess is the order in my book. But, that's what the forum is for, so we can all learn. I'm very thankful for your help and input.

Replies are listed 'Best First'.
Re^3: Sorting array, getting modification of read-only value error
by GrandFather (Saint) on Nov 29, 2012 at 23:20 UTC

    We are not at all annoyed to see someone trying, especially when they consider and make use of the advice they are given!

    Actually I'd put neatness first because with a little practice syntax and logic both flow from it and if it is neat at least someone else may be prepared to give a hand with tricky bits.

    There isn't enough context to show a "tidy" version of your code, but the following sample extrapolated somewhat from what you seem to be doing may be of interest. Note that the code below makes use of Perl objects to avoid global variables and to avoid domain specific knowledge leaking too much between different areas of the code.

    #!/usr/bin/perl use strict; use warnings; use Carp qw(); package Character; sub new { my ($class, %props) = @_; # Validation of %props here as required Carp::confess "Characters require a 'name' property" if !$props{na +me}; return bless \%props, $class; } sub newFromStr { my ($class, $str) = @_; my $config = extract($str); return $class->new(%$config); } sub newFromFile { my ($class, $filename) = @_; open my $fin, '<', $filename or Carp::confess "Can't open $filenam +e: $!\n"; my $str = do {local $/; <$fin>}; close $fin; return $class->newFromStr($str); } sub extract { my ($str) = @_; require 'YAML.pm'; return YAML::Load($str); } # subs defining what a Character is here sub Name { my ($self) = @_; return $self->{name}; } sub SetAttributes { my ($self, %attribs) = @_; for my $attrib (keys %attribs) { my ($rolls, $die) = $attribs{$attrib} =~ /(\d+)d(\d+)/i or nex +t; $self->{$attrib} = 0; $self->{$attrib} += int(1 + rand($die)) for 1 .. $rolls; } } package Game; sub new { my ($class, %props) = @_; # Validation of %props here as required return bless \%props, $class; } sub AddCharacter { my ($self, $character) = @_; my $name = $character->Name(); Carp::confess "Character name must be unique. $name is already in +use!" if exists $self->{characters}{$name}; $self->{characters}{$name} = $character; } sub LoadCharacters { my ($self, $source) = @_; opendir my ($scan), $source or Carp::confess "Can't open $source: +$!\n"; my @files = grep {-f $_} map {"$source\\$_"} readdir $scan; closedir $scan; foreach my $file (@files) { my $character = Character->newFromFile($file); $character->SetAttributes(hp => '1d20'); $self->AddCharacter($character); } } sub DumpCharacters { my ($self) = @_; my $format = "%-15s %5s %5s %5s\n"; printf $format, qw(Name init dex hp); for my $name (sort keys %{$self->{characters}}) { printf $format, $name, @{$self->{characters}{$name}}{qw(init d +ex hp)}; } } package main; my $testFolder = "./Characters"; createTestCharacters($testFolder); my $game = Game->new(); $game->LoadCharacters($testFolder); $game->DumpCharacters(); sub createTestCharacters { my ($folder) = @_; my @characters = ( {name => 'Red', init => 5, dex => 10}, {name => 'Spotty', init => 8, dex => 15}, {name => 'Night Runner', init => 15, dex => 17}, ); require 'YAML.pm'; for my $character (@characters) { my $name = $character->{name}; YAML::DumpFile("$folder\\$name.yaml", $character); } }

    Prints:

    Name init dex hp Night Runner 15 17 14 Red 5 10 16 Spotty 8 15 10
    True laziness is hard work