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

Re: transforming strings to an array of characters

Thread Previous | Thread Next
Paul Johnson
December 9, 2002 09:08
Re: transforming strings to an array of characters
Message ID:

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

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

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About