penny1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm extremely new in perl and currently having some problems on extracting the match names within ONLY certain rows in a text file.

For example of my text file :

partnm_1

prlot

prwafer

prbin

sspec

fab

partnm_2

sspec

fab

partnm_3

....

....

My intention is to print out prlot prwafer prbin in an .csv file in a column row by row according to the part number (partnm). For partnm_2 which has no prlot prwafer prbin, i want it to print "empty" in the column .

Any suggestion that i could match the name within certain lines of words ? I'm able to print the match values in excel file. but can't make it to search within certain rows. Appreciate much for the help .

Replies are listed 'Best First'.
Re: Print Match name for certain rows
by tangent (Parson) on Jan 07, 2014 at 15:50 UTC
    Here is a somewhat long-winded solution which I hope is self-documenting. Assumes input file is something like what you can see in the __DATA__ section - you will need to replace <DATA> with your opened filehandle:
    use strict; use warnings; use Data::Dumper; my (@results,%hoh,$current); while (my $line = <DATA>) { chomp $line; next unless $line; my ($col_name,$rest) = split(/\s/,$line,2); if ($col_name =~ m/^partnm_/) { push(@results,$col_name); $hoh{$col_name}{'partnm'} = $rest; $current = $col_name; next; } next unless $current; $hoh{$current}{$col_name} = $rest; } print Dumper(\%hoh); # to see what we've got so far my @cols = qw( partnm prlot prwafer prbin sspec fab ); my $hline = join(',',@cols); print "$hline\n"; for my $id (@results) { my $hash = $hoh{$id}; my @elems; for my $col (@cols) { my $val = $hash->{$col} || 'empty'; push(@elems,$val); } my $line = join(',',@elems); print "$line\n"; } __DATA__ partnm_1 101 prlot prlot_1 prwafer prwafer_1 prbin prbin_1 sspec sspec_1 fab fab_1 partnm_2 102 sspec sspec_2 fab fab_2 partnm_3 103
    Output:
    partnm,prlot,prwafer,prbin,sspec,fab 101,prlot_1,prwafer_1,prbin_1,sspec_1,fab_1 102,empty,empty,empty,sspec_2,fab_2 103,empty,empty,empty,empty,empty
Re: Print Match name for certain rows
by littlemonk (Sexton) on Jan 07, 2014 at 12:06 UTC

    try this and let me know

    #i just considered this is input file partnm_1 fdfg prlot dfg test line prwafer df prbin dsdg sspec dgdfg fab dfgdfgg partnm_2 sdgdf sspec dfgdg fab dfgdf partnm_3 dfsd sould not print dont prnt this is wrong

    this is the perl part..let mw know if its not working

    open($in,"file.txt") || die "file cannot be opened..$!"; @arr =<$in>; if(@arr1=grep/(partnm_1|prlot|prwafer|prbin|sspec|fab|partnm_2|sspec|f +ab|partnm_3)/,@arr){ print "@arr1"; }
      open($in,"file.txt") || die "file cannot be opened..$!"; @arr =<$in>; if(@arr1=grep/(partnm_1|prlot|prwafer|prbin|sspec|fab|partnm_2|sspec|f +ab|partnm_3)/,@arr){ print "@arr1"; } foreach $line(@arr1){ @data =split/\s/,$line; push(@required,$data[0]); } print "@required";
Re: Print Match name for certain rows
by NetWallah (Canon) on Jan 07, 2014 at 15:12 UTC
    Do your input data sections (for each partnm) always end with "fab" ?

    If so, you could read and process the information for one part at a time by setting:

    $/="fab\n";

            If your eyes hurt after you drink coffee, you have to take the spoon out of the cup.
                  -Norm Crosby

Re: Print Match name for certain rows
by kcott (Archbishop) on Jan 08, 2014 at 09:59 UTC

    G'day penny1,

    Welcome to the monastery.

    Here's how you might go about extracting the data:

    #!/usr/bin/env perl -l use strict; use warnings; my ($part_no, @part_info); while (<DATA>) { if (/^partnm_(\d)/) { print_part_data($part_no, \@part_info); $part_no = $1; @part_info = (); next; } push @part_info, $1 if /^(prlot|prwafer|prbin)/; } print_part_data($part_no, \@part_info); sub print_part_data { my ($num, $info) = @_; print "$num: ", @$info ? join ',' => @$info : 'empty' if defined $ +num; } __DATA__ partnm_1 prlot prwafer prbin sspec fab partnm_2 sspec fab partnm_3 .... ....

    Output:

    1: prlot,prwafer,prbin 2: empty 3: empty

    You say "I'm able to print the match values in excel file.": you should use the code you're currently using to do this in print_part_data() (I'm just printing to the screen for demo purposes).

    -- Ken