# With split my $code = q(fruit:red:apple); my @parts = split /:/, $code; pop @parts; my $parent = join ':', @parts; # With substr and rindex (my favourite, and probably the fastest) my $code = q(fruit:red:apple); my $parent = substr($code, 0, rindex($code, ':'));