Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: please help me to resolve the Line comments and appending issue

by aitap (Curate)
on Aug 09, 2012 at 10:43 UTC ( [id://986480]=note: print w/replies, xml ) Need Help??


in reply to Re^2: please help me to resolve the Line comments and appending issue
in thread please help me to resolve the Line comments and appending issue

Here is the code based on your example:
#!/usr/bin/perl use warnings; use strict; use Getopt::Std; my %options; getopt("ivcm",\%options); do { die "Usage: $0 -i <input file> -v <variable file> -c <code file> -m < +macro file>\n" unless defined $options{$_} } for qw/i v c m/; open my $input, "<", $options{i} || die "$options{i}: $!\n"; open my $code, ">", $options{c} || die "$options{c}: $!\n"; open my $var, ">", $options{v} || die "$options{v}: $!\n"; open my $macro, ">", $options{m} || die "$options{m}: $!\n"; while (defined(my $line = <$input>)) { chomp $line; next if $line=~/^\s*$/; next if $line=~/^\s*\*/; $line=~s/EQU\s+\*//; $line=~s/\s*$//; my @fields = split /\s+/,$line,4; shift @fields until $fields[0] ne ''; if ($fields[0] eq 'BEGIN') { print $code join ("\t",@fields),"\n"; } elsif ($fields[0] =~ /^(BEGIN|L|CLI|BNE|LTR)$/) { #code pop @fields; print $code (join "\t",@fields),($fields[0] eq 'CLI' ? "" : "\ +n"); } elsif ($fields[0] =~ /^@/) { # macro print $macro (join "\t",@fields),"\n"; pop @fields; print $code (join "\t",@fields),"\n"; } elsif ($fields[1] =~ /^D[CS]$/) { #variable? my ($len) = ($fields[2] =~ /L(\d+)/); #??? my ($type) = $fields[2] =~ /^(.)/; # ??? my ($value) = ($fields[2] =~ /'(\d+)'$/); #??? print $var (join "\t",$fields[0],$len,$type,$value,$fields[3]) +,"\n"; } else { warn "Don't know what to do with line\n$line\n"; } } close $_ || die $! for ($input,$code,$var,$macro);
It sure contains some mistakes because I had to guess some things about CLI, BNE and variables. Feel free to fix it. If you would state more clearly how to distinguish between code and variables, it will be easier to write the proper code.
See split, join, shift, pop for more info on this script.
Sorry if my advice was wrong.

Replies are listed 'Best First'.
Re^4: please help me to resolve the Line comments and appending issue
by suno (Acolyte) on Aug 10, 2012 at 12:51 UTC
    hi, ...

    i am not still clear with the macro file..

    my requirement is :

    I have hardcoded all keywords to an array(keywords come only at the begining of the sentence).I want to capture all words other than the keywords and push them to the macro file.

    So i thought of comparing each word in my input file(only the first word) with the keyword array and if it is not present in the array then push it to the macro file..

    in the above example : CLI is a keyword ...where else @PUT is a macro

    but i couldnt able to figure it out clearly.Can you please help me out in this ?

      Array search can be done using grep, but if you want just to check whether an item exists in the list, using hash is faster:
      my %keywords = map { $_ => 1 } qw/L CLI BNE LTR .../; ... #later in the code if (exists $keywords{$whatever_is_suspected_to_be_a_keyword}) { # it is a keyword ... } ...
      Sorry if my advice was wrong.

        Almost always true in practice. But there is more overhead in a hash than in an array, and that's magnified if you already have an array and have to duplicate it as a hash. If you're going to check more than one value against the list, as in the FAQ "How do I search file2 for all the values in file1?" then a hash is almost certainly the best solution. But if you only have to check one or two values, you may be better off sticking with grep:

        % cat 986735.pl #!/usr/bin/env perl use Modern::Perl; use Benchmark qw(:all); my @words = split /\s+/, `cat bigfile`; #8.5MB file say scalar @words, " words in bigfile"; #1.3M words my $match = 'professional'; # appears 16 times scattered through bigfi +le cmpthese( 10, { 'hash it' => \&hashit, 'grep it' => \&grepit, 'first it' => \&firstit, }); sub hashit { my %h; @h{@words} = (); my $exists = exists $h{$match}; } sub grepit { my $exists = grep { $_ eq $match } @words; } sub firstit { use List::Util 'first'; my $exists = first { $_ eq $match } @words; } % perl 986735.pl 1293687 words in bigfile Rate hash it grep it first it hash it 3.33/s -- -43% -89% grep it 5.80/s 74% -- -81% first it 29.8/s 795% 413% --

        So if I only need to search the list once, grep wins over a hash. For multiple searches, a hash comes out ahead. List::Util's first() routine splits the difference a little; with my dataset it beats the hash for up to about 8 searches, but I assume that would vary greatly depending on how early in the array the match is made. It'd take more testing with matches found earlier/later/never in the array to form a good comparison there.

        My simple conclusion would be: always build a hash unless you know your program will only search it once; then use grep or List::Util::first. Also, if I'm pulling data in from somewhere (like a file) and I know I'm going to be searching it this way, I put it straight into a hash from the start.

        Aaron B.
        Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found