in reply to Problem in extracting alphanumeric string from file

#!/usr/bin/perl use strict; use warnings; while (<DATA>) { next if ! /^\w{4}(\.\d+){3}/; print; } __DATA__ MONDAY MD21.2.7.8 TUESDAY # Driver Code = TD20.2.7.8 TD20.2.7.8 WEDNESDAY # Driver Code = WD20.2.7.8 WD30.2.7.8 FRIDAY # Driver Code = FD20.2.7.8 FD40.2.7.8

Prints:

MD21.2.7.8 TD20.2.7.8 WD30.2.7.8 FD40.2.7.8

I presume the scalar @t in your code is in response to the warning:

Applying pattern match (m//) to @t will act on scalar(@t) at ...

The real problem is @t =~ /.../ doesn't make sense. The =~ operator does not work with arrays or hashes. Actually, the real problem is that you need to match against the entire line so you don't need the split and don't need the array. See the regex match in my sample code to see how you ought to do the match.

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: Problem in extracting alphanumeric string from file
by rahulme81 (Sexton) on Aug 19, 2015 at 08:45 UTC
    Thank you so much ... I am Starter with perl.. Learnt today - The =~ operator does not work with arrays or hashes. Thanks Again.

      It doesn't work on the hash or array container, but does work with their scalar elements:

      use warnings; use strict; my %hash = (one => 1, two => 2); for my $key (keys %hash){ print "$key\n" if $key =~ /one/; } my @array = qw(one two three); for my $elem (@array){ print "$elem\n" if $elem =~ /one/; }
      Sorry while testing I found one issue - if I increase the strings that gives me only first match. For tuesday this prints only TD20.2.7.8
      MONDAY MD21.2.7.8 TUESDAY # Driver Code = TD20.2.7.8 TD20.2.7.8 TD20.2.7.7 TD20.2.7.9 WEDNESDAY # Driver Code = WD20.2.7.8 WD30.2.7.8 WD30.2.7.9 FRIDAY # Driver Code = FD20.2.7.8 FD40.2.7.8
        Achieved - condition changed to next if ! /^(\w*){4}(\.\d*){3}/;