in reply to Tree path analyzer regex problem (maybe other issues)?

Given that you have fixed strings, eq and ne are likely better choices than regular expressions. Also, note that your code has assumptions about depth in it - the fact that you had to turn off warnings is usually a sign you have a design issue. Perhaps something more like:

use strict; use warnings; use Data::Dumper; my @last = (); while(<DATA>) { chomp; my @paths = split /\|/; my $different = 0; foreach my $i (0 .. $#last) { last if $i > $#paths; if ($different or $last[$i] ne $paths[$i]) { $different = 1; print join('|',@paths[0 .. $i]),"\n"; } } foreach my $i ($#last+1 .. $#paths) { print join('|',@paths[0 .. $i]),"\n"; } @last = @paths; } __DATA__ Valentine's Day Valentine's Day|Cards Valentine's Day|Flowers Valentine's|Candy Valentine's|Telegrams

Replies are listed 'Best First'.
Re^2: Tree path analyzer regex problem (maybe other issues)?
by bryank (Acolyte) on Jun 23, 2009 at 16:50 UTC
    Thanks -- that does exactly what I want. Can you explain what some of the magic does? Specifically:

    foreach my $i (0 .. $#last)

    and

    foreach my $i (0 .. $#last)

      I would not describe that as magic. Range Operators (written as ..) produce a list of integers between the two limits. As described in Variable names, $#array returns the index of the last element of @array. "Magic" usually involves (from my perspective) either action-at-a-distance or Perl typing in the background. These are just operators.