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

Hi, I need guidance with this perl script . I am trying to print a block of lines (say 5 lines at a time). I do have a start line and an end line. I have to look into multiple flat files from which I will be accessing that block. my block of code that I have written is as follows:

open(MYINPUTFIL3, <DATA>); MYINPUTFIL3: my $spool1 = 0; my @a; while (<MYINPUTFIL3>) { if (/start line:/i) { $spool1 = 1; next; } elsif (/End Line:/i) { print map { "$_" } @a; $SYS=$_; $SYS =~ s/\r|\n//g; $SYS=~ s/^\s+//; $SYS=~ s/\s+$//; $SYS1 = $SYS; $SYS2 = $SYS; $SYS2 = s/[^a-zA-Z]*//g; $SYS1 =~ s/[^0-9.]*//g; @a = (); } if ($spool1) { push (@a, $SYS); $SYS=$_; $SYS =~ s/\r|\n//g; $SYS=~ s/^\s+//; $SYS=~ s/\s+$//; $SYS1 = $SYS; SYS2 = $SYS; $SYS2 = s/[^a-zA-Z]*//g; $SYS1 =~ s/[^0-9.]*//g; last; } } close DATA;

Let me give example of input and output data here:

Input Data:

id1 : /a text lies here/

add name: new add1

ph no: new phone1

country: new country1

id2 : /some other text lies here/

add name: new add2

ph no: new phone2

country: new country2

Desired output:

id2:/some other text lies here/ add name: new add2 ph no: new phone2 country: new country2

So my question is as follows.

1. it prints the details for the Id1 and not Id2 even though I am giving id2 in the if statement (for start Line)

2. I want next 4 lines to be printed. But it prints only one line after the start line.

3. I get output which is distorted(with tabs and new line in it)

Please guide...Stuck....

Replies are listed 'Best First'.
Re: printing alternate lines from a file
by NetWallah (Canon) on Sep 12, 2012 at 04:46 UTC
    If your data is absolutely regular, 4 records at a ttime, you could use something like this to simplyfy processing, in your read loop:
    open my $file, "<" , "Filename" or die "$!"; my @buf; while (<$file>){ chomp; s/^\s*//; # Drop leading blanks s/\s*$//; # Drop trailing blanks if (0 == $. % 4){ # Processes the contents of @buf # it appears that something like this is what you need: print join(" ",@buf, $_), "\n"; @buf=(); # Reset it }else{ push @buf, $_; } } close $file; @buf and print @buf; # In case there is left-over stuff.
    Usual 'untested' disclaimer.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Thanks NetWallah for the reply..
Re: printing alternate lines from a file
by kcott (Archbishop) on Sep 12, 2012 at 07:51 UTC

    G'day perl_req,

    Welcome to the monastery.

    It would appear you're not really across how to open files in Perl. Take a look at open. Note how your code looks nothing like the examples given there. Compare with NetWallah's code (above) which does follow the documented format. (You're also closing the wrong filehandle - see close.)

    Perl will tell you about various problems with your code if you ask it to. Add use strict; and use warnings; near the top of your code. If you find the messages it produces are a little hard to understand, also add use diagnostics; for longer, more descriptive explanations. (See: strict, warnings and diagnostics.)

    Thanks for providing sample input. Unfortunately, you've put each line in its own HTML paragraph (<p>...</p>) which converts all strings of whitespace (spaces, tabs, newlines) to a single space. That's a lot of extra work for you but of no benefit to us: we can't see where the extra whitespace you've described occurs ("... output which is distorted ..."). For future reference, put the whole block of input (output, etc.) within <code>...</code> tags as you did with your code listing: less work for you, more useful for us.

    Here's a solution which produces your desired output. I've added arbitrary tabs to the input to simulate the problem which I can't see (as just described).

    #!/usr/bin/env perl use strict; use warnings; my $lines_per_block = 4; my $wanted_id = 'id2'; while (my $joined_block = join_block_of_lines($lines_per_block)) { next unless $joined_block =~ /^$wanted_id/; print $joined_block, "\n"; } sub join_block_of_lines { my ($lines_per_block) = @_; my @block_lines; for (1 .. $lines_per_block) { my $line = <DATA>; return '' unless defined $line; $line =~ s/\s*\n$//; push @block_lines, $line; } my $joined_line = join ' ' => @block_lines; $joined_line =~ s/\s*\t\s*/ /g; $joined_line =~ s/^(\S+)\s+(:)\s+/$1$2/; return $joined_line; } __DATA__ id1 : /a text lies here/ add name: new add1 ph no: new phone1 country: new country1 id2 : /some other text lies here/ add name: new add2 ph no: new phone2 country: new country2

    Output:

    $ pm_join_block_of_lines.pl id2:/some other text lies here/ add name: new add2 ph no: new phone2 c +ountry: new country2

    -- Ken

      Hi Ken, Thank you so much for your reply. I have been trying couple of things but I am stuck at one of them. I will post the question soon if I am not able to solve it myself first. Thanks