in reply to parse for string, then print unique
You're not getting $1 set because there is no capture done in the match (misplaced parens). If there was, you'd still be misusing $1, since it will just repeat from the previous match if the current try fails.
By "strings which start with a dollar sign", what do you mean? What ends the strings you want? I'll assume you want to pick out perl variables of the ordinary kind (no punctuation vars or other symbol table stuff).
That only catches the first instance in a line. If you want to catch them all there is a neater notation,my @in; while (<>) { push @in, $1 if /(\$[A-Za-z_]\w*)/; }
which takes advantage of placing the match in list context.while (<>) { push @in, /(\$[A-Za-z_]\w*)/g; }
Once you have your array, uniqueness is gotten by the usual trick:
my %hsh; @hsh{@in} = (); print $_, $/ for keys %hsh;
Update: Seeing welchavw's way of doing it all at once, I'd rewrite as
Note that using /g is not a prerequisite of placing the match directly in list context to get the values. The push @in, $1 if /(\$[A-Za-z_]\w*)/; line could have been written, push @in, /(\$[A-Za-z_]\w*)/; and pushing an empty list does not grow the array.my %hsh; @hsh{ /(\$[A-Za-z_]\w*)/g } = () while <>; delete $hsh{''}; # tidy up print $_, $/ for keys %hsh;
After Compline,
Zaxo
|
|---|