This code as it is is useless, unless we print the content or do something before making the copy but can provide a way to to duplicate/reorder stacks.

Maybe looping over

  if (defined $xavp(x[$var(i)]))

Is sufficient, no need to pv_unset