Re: first post
am 21.07.2011 15:03:26 von Shlomi FishHi,
On Wed, 20 Jul 2011 22:30:42 -0700
"John W. Krahn"
> H Kern wrote:
> > Hi,
>=20
> Hello,
>=20
> > 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"} =3D split(/\t\n/,
> > @header{"info"} =3D split(/\t\n/,
>=20
>=20
> The hash slice @header{"keys"} acts the same as a list and enforces list=
=20
> context on the assignment while the scalar $header{"keys"} forces scalar=
=20
> context on the assignment and as the documentation says:
>=20
>=20
> perldoc -f split
>=20
> 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.)
>=20
> 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.
>=20
>=20
> So you need to use a scalar value but in list context:
>=20
> ( $header{ keys } ) =3D split /\t\n/,
> ( $header{ info } ) =3D split /\t\n/,
>=20
> Or use a list slice on the right-hand side of the assignment:
>=20
> $header{ keys } =3D ( split /\t\n/,
> $header{ info } =3D ( split /\t\n/,
>=20
Maybe he instead wants:
$header{keys} =3D [ split /\t\n/,
However, there is one problem where
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.
>=20
>=20
>=20
>=20
> John
--=20
------------------------------------------------------------ -----
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.
â=94 Sjors, http://dazjorz.com/
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/