Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: parse for string, then print unique

by welchavw (Pilgrim)
on Dec 16, 2003 at 04:49 UTC ( #314973=note: print w/ replies, xml ) Need Help??


in reply to parse for string, then print unique

It isn't working for a multiplicity of reasons. First off, your regex is capturing nothing in $1, so you shouldn't be using that var. In same regex, your ^ is misused - ^ should be placed so that it anchors the front of the line - it cannot be preceeded by \b and be sanely used. Your push of @in is unconditional, which is also wrong - you only want to push if the regex matches. Further, you should be using a hash to collect uniq values, not an array. I don't even begin to understand what that grep is trying to do.

This code should be a good start...

#!/usr/bin/perl use Data::Dumper; while (<DATA>) { $in{$1} = 1 if ($_ =~ (/^\s*(\$\w+.*)/)); } @a = keys %in; print Dumper \@a; __DATA__ $first = 1; $first = 1; $second = 2; #some comment # $third = 3;

Please also read this link Perl Idioms Explained - keys %{{map{$_=>1}@list}}.


Comment on Re: parse for string, then print unique
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://314973]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2015-07-07 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls