Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Help please, or Urgent help needed!

by anakin30 (Acolyte)
on Mar 27, 2012 at 18:31 UTC ( #961993=perlquestion: print w/ replies, xml ) Need Help??
anakin30 has asked for the wisdom of the Perl Monks concerning the following question:

Description

This perl script will provide grep-like functionality to return multi-line (record) matches for

blank-line deliminated files such as LDIF files used by LDAP. A match on any line will return

all lines from the previous blank line until the following one.

here this is the script

----------------------------------------------

#!/usr/bin/perl -w use strict; my $search = shift; my $file = shift; my $out = shift; if (! $file) { open(INF, '-') or die("Unable to open STDIN\n"); } else { open(INF, $file) or die("Unable to open $file\n"); } my @buffer; while (<INF>) { my $line = $_; if ($line eq "\n") { my $count = 0; #@buffer = ""; undef @buffer; } else { $buffer[$#buffer+1] = $line; } if (/$search/io) { my $buf; foreach $buf (@buffer) { print $buf; } while (($out = <INF>) ne "\n") { print $out; } undef @buffer; print "\n"; } }

----------------------------------------------

when execute the script, it gives me this error message below, please help me to correct the script to avoid the error below as well as display as per format i want below

Use of uninitialized value in print at ./ldifgrep line 30, <INF> line 24.

Use of uninitialized value in string ne at ./ldifgrep line 30, <INF> line 24.

Use of uninitialized value in print at ./ldifgrep line 30, <INF> line 24.

Use of uninitialized value in string ne at ./ldifgrep line 30, <INF> line 24.

Use of uninitialized value in print at ./ldifgrep line 30, <INF> line 24.

Use of uninitialized value in string ne at ./ldifgrep line 30, <INF> line 24.

the line 59 is at this line ---> while (($out = <INF>) ne "\n")

----------------------------------------------

check out the sample LDIF File below I need to grep the full dn: and the next line attribute mail only

dn: uid=testing,ou=comp,o=test.com dpwnc: BH mail: example@great.com sn: example dn: uid=testing1,ou=comp,o=test.com dpwnc: CH mail: example1@great.com sn: example1 dn: uid=testing2,ou=comp,o=test.com dpwnc: DH mail: example2@great.com sn: example2

i want the output generated into new file with the format below

dn: uid=testing,ou=comp,o=test.com mail: example@great.com dn: uid=testing1,ou=comp,o=test.com mail: example1@great.com dn: uid=testing2,ou=comp,o=test.com mail: example2@great.com

Comment on Help please, or Urgent help needed!
Select or Download Code
Re: Help please, or Urgent help needed!
by SuicideJunkie (Priest) on Mar 27, 2012 at 18:55 UTC
    ...ldifgrep line 59...

    I don't see a print on line 59 out of 36 of your code listing. Perhaps you aren't running the script you think you are running?

    To debug uninitialized warnings, try putting an if statement near the line in question; if (not defined $var) then print out all of your variables to see why the value became undefined in the first place.

      i have re-updated perl script code, pointing line to for the error..

        How about reading with block. If you set input separator with empty string, you will get block.
        #!/usr/bin/perl use strict; use warnings; open( my $fh, '<', "ldif.file") or die $!; $/=""; while( my $item=<$fh> ){ print "===========\n"; print "$item\n"; print "===========\n"; if ($item =~ /something/is ){ print "as you like\n"; } } close $fh; __DATA__ =========== dn: uid=testing,ou=comp,o=test.com dpwnc: BH mail: example@great.com sn: example =========== =========== dn: uid=testing1,ou=comp,o=test.com dpwnc: CH mail: example1@great.com sn: example1 =========== =========== dn: uid=testing2,ou=comp,o=test.com dpwnc: DH mail: example2@great.com sn: example2 ===========
Re: Help please, or Urgent help needed!
by MidLifeXis (Prior) on Mar 28, 2012 at 13:18 UTC

    Consider this block of code in your original example:

    while (($out = <INF>) ne "\n") { print $out; }
    If you happen to at the end of file, <INF> will return undef, which is ne "\n". Infinite loop.

    --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961993]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2014-09-24 01:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (243 votes), past polls