develooper Front page | perl.beginners | Postings from December 2002

Re: transforming strings to an array of characters

Thread Previous | Thread Next
From:
Paul Johnson
Date:
December 9, 2002 09:08
Subject:
Re: transforming strings to an array of characters
Message ID:
34022.193.134.254.145.1039453729.squirrel@wesley.pjcj.net

Mystik Gotan said:
> If not using length() direcly on @world_list:
>
> @word_list = qw(bla bla bla bla bla joah hey);

Your following code is an expensive noop ...

> foreach $element (@word_list) {
> local($content);

Whoops.  Why are you using local?  Use my unless you _know_ that you need
local.

> my(@lengths);

Declaring @lengths within the loop means that you cannot see it outside,
and that you get a new array each time round the loop anyway.

> $content = length($element);
> push(@lengths, $element); # or different order, I always forgot this :)
> }

Try something like:

  my @lengths = map { length } @word_list;

-- 
Paul Johnson - paul@pjcj.net
http://www.pjcj.net




Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About