H Kern wrote:
Hi,
Hello,
My first newbie post. I wish to have two arrays indexed by a hash table. The simple program below runs and behaves properly initializing the hash table with the information I wish it to have. However, Perl generates the following suggestion on the @header{} assignment statements, "Scalar value @header{"keys"} better written as $header{"keys"} at iifm.pl line..." If I rewrite it as Perl suggests, the two %header{} elements get initialized to the size of the arrays instead of the arrays. Why does Perl make this suggestion, and how do I get rid of it without getting rid of the "use warnings" statement? Thanks, --H use strict; use warnings; my %header; open( IN, "<", $ARGV[0] ); @header{"keys"} = split(/\t\n/, <IN>); @header{"info"} = split(/\t\n/, <IN>);
The hash slice @header{"keys"} acts the same as a list and enforces list context on the assignment while the scalar $header{"keys"} forces scalar context on the assignment and as the documentation says:
perldoc -f split split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/ split Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.) In scalar context, returns the number of fields found. In scalar and void context it splits into the @_ array. Use of split in scalar and void context is deprecated, however, because it clobbers your subroutine arguments. So you need to use a scalar value but in list context: ( $header{ keys } ) = split /\t\n/, <IN>; ( $header{ info } ) = split /\t\n/, <IN>; Or use a list slice on the right-hand side of the assignment: $header{ keys } = ( split /\t\n/, <IN> )[ 0 ]; $header{ info } = ( split /\t\n/, <IN> )[ 0 ]; John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/