Hi,

On Wed, 20 Jul 2011 22:30:42 -0700
"John W. Krahn" <jwkr...@shaw.ca> wrote:

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

Maybe he instead wants:

$header{keys} = [ split /\t\n/, <IN> ];

However, there is one problem where <IN> will return a single line, and so
there will only be one "\n" at most, so I don't understand what he wants to
split exactly. Does he want to remove \t\n from the end of the line?

Regards,

        Shlomi Fish.

> 
> 
> 
> 
> John



-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
What Makes Software Apps High Quality -  http://shlom.in/sw-quality

bzr is slower than Subversion in combination with Sourceforge.
    — Sjors, http://dazjorz.com/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to