Hi Roman,

Roman wrote:

> use strict;
>
> my $file = shift;
> my $line;
>
>
> open(IN, "<$file") || die "$!";
> while($line =<IN>)
> {
>    $line =~ /(\$\S+):.(\S+)/is;
>
>    print "match1: $1; match2: $2 \n";
> }

I would redefine the record separator for your while loop to process each of the four records in your example as a whole unit, instead of going line-by-line. You can then write a regular expression to capture the path and the file name.

#!/usr/bin/perl

use strict;
use warnings;

local $/ = "\n\n";  # record separator = two consecutive line breaks

while (<>) {
        m/      ^               # beginning of line
                (               # start capture
                        [^:]+   # capture all non-colon chars
                )
                :               # colon
                \s+             # space chars, including line break
                (               # second capture
                        [^.]+   # one or more non-period chars
                        \.      # a period
                        \S+     # one or more non-space chars
                )
        /x;                     # m/^([^:]+):\s+([^.]+\.\S+)/;
        print $1,'/',$2,"\n";
}

Using shift to set $file to the file name and explicitly defining $line are not necessary. This script does the same thing as yours. This is Perl's way of making things easier for you. ;)

I would also suggest you 'use warnings' when writing code. It'll catch a lot of errors for you, such as a 'Use of uninitialized value in concatenation (.) or string' in your code. You can get a (partial) explanation by adding a 'use diagnostics' line to the beginning of your program.

Best,

Damon


--

Damon Allen DAVISON
http://www.allolex.net

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to