in reply to Iterating through Two Arrays. Is there a better use of memory?

If you're working with strings, then work with strings. A string uses less memory than an array of characters, and string operations are fast:

my $line1 = 'CABGFE'; my $line2 = 'DBF'; say for $line1 =~ /[$line2]/g;
Even if you're working with arrays of characters, joining into strings will probably be faster:
my @line1 = qw(C A B G F E); my @line2 = qw(D B F); my $line1 = join '', @line1; my $line2 = join '', @line2; say for $line1 =~ /[$line2]/g;

If you've only simplified your example to arrays of characters, but they're really arrays of something else, then use a hash, as others have already suggested.

Replies are listed 'Best First'.
Re^2: Iterating through Two Arrays. Is there a better use of memory?
by Jeri (Scribe) on Oct 18, 2011 at 17:41 UTC

    Hi, thanks for replying. I've decided to work with strings instead of arrays. Thanks for the advice. However, I can't get your code to work properly.

    say for $line1 =~ /[$line2]/g;

    it complains...

    Bareword "say" not allowed while "strict subs" in use at evan.pl line +19.

    when I delete say, it complains...

    syntax error at evan.pl line 19, near "$line1 =~"

    What's the trick to this sting comparison?

    thanks!