Chaps,
I am testing all your code one by one, Appreciate your time and detailed
inputs.

Many Thanks
Sj




On Fri, Aug 23, 2013 at 6:01 PM, Jim Gibson <jimsgib...@gmail.com> wrote:

>
> On Aug 23, 2013, at 9:06 AM, jet speed wrote:
>
> > Chaps,
> >
> > Please i need help on the regular expression, i have the sample code
> below.
> > I only want to match the entries from the array to the file and print
> the matching line
> >
> > for example if i only want to match fc3/23, in my code it prints both
> the lines fc3/2 and fc3/23. How to restrict to exact matches and print only
> ex: fc3/23
> >
> >
> >
> > out.txt
> > ------
> >
> > fc3/2  10:00:00:00  host1
> > fc3/23 10:00:00:00  host2
> > fc10/1 10:00:00:00  host3
> > fc10/11 10:00:00:00  host4
> > fc3/1 10:00:00:00  host5
> > fc3/14 10:00:00:00  host6
> > fc12/1 10:00:00:00  host7
> > fc12/12    10:00:00:00  host8
> >
> >
> > sample code
> > -----------------
>
> The code below does not compile. Please make sure you post valid programs
> if you want help.
>
> >
> > my @check = (fc3/23, fc10/1, fc3/14, fc12/12);
>
> You need to put quotes around strings:
>
> my @check = ('fc3/23', 'fc10/1', 'fc3/14', 'fc12/12');
>
>
> > my $f2 = 'out.txt';
> > for my $element(@check) {
>
> You should indent blocks (for, while) for readability.
>
>
> > open my $fh2, '<', $f2 or die "could not open $f2: $!";
>
> Here, you are opening the file for each iteration. Reading files is much
> slower than iterating over an array. Therefore, if you have to 1) read a
> file and 2) iterate over an array, you should reverse the order of
> operations: Read a line from the file and then iterate over the array for
> each line in the file.
>
> > while (my $line = <$fh2>) {
> > chomp $line;
> > if ($line = / ^(fc\d+\/\d+/) {
>
> This line has three syntax errors:
> 1. You want the binding operator =~ instead of assignment =
> 2. You don't want a space between / and ^
> 3. The final / and closing ) should be swapped.
>
> I am assuming you mean:
>
>   if ($line =~ /^(fc\d+\/\d+)/ {
>
>
> > $match=$&;
>
> Since your regular expression captures the entire match, you should use
> the capturing variable $1 instead of the whatever-matched variable $&. It
> is actually faster.
>
> Every line of your input will match the above regular expression, so the
> only effect is to extract the first field. You can do that by splitting the
> line on whitespace:
>
>   my( $first, @rest ) = split(/ /,$line);
>
> > }
> > if  ($element =~ $match) {
>
> You need slashes or the m operator to define a regular expression:
>
>   if( $element =~ /$match/ ) {
>
> If you want to match the entire string, you need to add beginning and
> ending anchors:
>
>   if( $element =~ /^$match$/ ) {
>
> But if that is what you want, just use eq:
>
>   if( $element eq $match ) {
>
>
> > print "$line \n";
> > }
> > }
> > }
> >
> >
> > required output
> > --------------------
> >
> > fc3/23 10:00:0:00  host2
> > fc10/1 10:00:0:00  host3
> > fc3/14 10:00:00:00  host6
> > fc12/12    10:00:00:00  host8
> >
> > Thanks
> > Sj
> >
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to