Re: first post

Re: first post

am 21.07.2011 15:03:26 von Shlomi Fish

Hi,

On Wed, 20 Jul 2011 22:30:42 -0700
"John W. Krahn" wrote:

> 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/, )[ 0 ];
> $header{ info } =3D ( split /\t\n/, )[ 0 ];
>=20

Maybe he instead wants:

$header{keys} =3D [ split /\t\n/, ];

However, there is one problem where 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.

>=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/

Re: first post

am 21.07.2011 15:29:28 von Shawn H Corey

On 11-07-21 09:03 AM, Shlomi Fish wrote:
> However, there is one problem where 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?

His file is probably a tab-delimited text one and doesn't realize he
should be using Text::CSV to parse it.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

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

Re: first post

am 21.07.2011 17:41:55 von Rob Dixon

On 21/07/2011 14:03, Shlomi Fish wrote:
>
> However, there is one problem where 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?

I posted much earlier to comment on this.

I am pretty sure that the original code is a perversion of

split /\t|\n/;

which is a lazy way of losing a trailing newline without chomping first.

Rob

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

Re: first post

am 21.07.2011 17:52:32 von Shawn H Corey

On 11-07-21 11:41 AM, Rob Dixon wrote:
> I am pretty sure that the original code is a perversion of
>
> split /\t|\n/;
>
> which is a lazy way of losing a trailing newline without chomping first.

It also seems excessive. This is the same thing:

split /[\t\n]/;


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

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

Re: first post

am 22.07.2011 17:08:36 von Rob Dixon

On 21/07/2011 16:52, Shawn H Corey wrote:
> On 11-07-21 11:41 AM, Rob Dixon wrote:
>> I am pretty sure that the original code is a perversion of
>>
>> split /\t|\n/;
>>
>> which is a lazy way of losing a trailing newline without chomping first.
>
> It also seems excessive. This is the same thing:
>
> split /[\t\n]/;

Aside from your regex being longer, I see little to choose between the
two equivalents. One may be slightly faster to execute than the other,
but I wouldn't like to bet on which one, and I hardly see the former as
'excessive'.

On balance I would probably the latter version in my own code, but I
wrote it this way as it was closest to the OP's code of

split /\t\n/;

which was probably incorrect and was confusing people.

Rob

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