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

Hi, I am trying to write a script to pull single value matching a certain pattern ( xxx-xxxx). This script is giving me a null output. Could someone help me this? or can suggest a simpler script to accomplish my requirement. The source entry contain several entries for any attribute, but my intention is to pull one that matches (xxx-xxxx). Here is my script :
#!/usr/bin/perl my @office_patterns = ('^\d{3}-\d{4}$'); my @international_patterns = ('^[+]\d \d{3} \d{3}-\d{4}$'); my $record = "662-5555"; my $record = "+1 102 892-1314"; my @office_phones; my $office_phones; my @international_phones; NUMBER: foreach $record { # Trim spaces. $record =~ s/^\s+//; $record =~ s/\s+$//; foreach $office_pattern (@office_patterns) { if ($record =~ /$office_pattern/) { push @office_phones, $record; next NUMBER; } } foreach $international_pattern (@international_patterns) { if ($record =~ /$international_pattern/) { push @international_phones, $record; next NUMBER; } } } print "$office_phones[0]", "\n";
Thanks, Pamela

Replies are listed 'Best First'.
Re: pull single value
by ikegami (Patriarch) on Nov 09, 2009 at 20:40 UTC

    This script is giving me a null output.

    Really? I get output

    syntax error at 806010.pl line 12, near "$record {" syntax error at 806010.pl line 29, near "}" Execution of 806010.pl aborted due to compilation errors.

    When I fix the error, it works fine for me:

    #!/usr/bin/perl use strict; use warnings; my @office_patterns = ( '^\d{3}-\d{4}$' ); my @international_patterns = ( '^[+]\d \d{3} \d{3}-\d{4}$' ); my @records = ( '662-5555', '+1 102 892-1314', ); my @office_phones; my $office_phones; my @international_phones; NUMBER: foreach (@records) { my $record = $_; # Trim spaces. $record =~ s/^\s+//; $record =~ s/\s+$//; foreach my $office_pattern (@office_patterns) { if ($record =~ /$office_pattern/) { push @office_phones, $record; next NUMBER; } } foreach my $international_pattern (@international_patterns) { if ($record =~ /$international_pattern/) { push @international_phones, $record; next NUMBER; } } } print "$_ (o)\n" for @office_phones; print "$_ (i)\n" for @international_phones;
    662-5555 (o) +1 102 892-1314 (i)

    Here's some cleanup code:

    #!/usr/bin/perl use strict; use warnings; my @office_pats = ( '\d{3}-\d{4}' ); my @iternat_pats = ( '[+]\d \d{3} \d{3}-\d{4}' ); my ($office_pat) = map qr/$_/, join '|', @office_pats; my ($internat_pat) = map qr/$_/, join '|', @internat_pats; my @office_phones; my @internat_phones; for (@recs) { my $rec = $_; $rec =~ s/^\s+//; $rec =~ s/\s+$//; push @office_phones, $rec if $rec =~ /^$office_pat\z/; push @internat_phones, $rec if $rec =~ /^$internat_pat\z/; } print "$_ (o)\n" for @office_phones; print "$_ (i)\n" for @internat_phones;
      Can u help me with this? What if i want to save whatever is matched.
      my $record = "662-5555"; my $record = "+1 102 892-1314"; while($record =~ m/(^\d{3}-\d{4}$)/) { print "$record"; }
      Thanks, Pamela

        Then go back to using push @array, $record;, like you did in the OP.

        Maybe you should address the warning before you do anything else.

        Then go back to using push @array, $record;, like you did in the OP.

        Maybe you should address the warning before you do anything else.

Re: pull single value
by bichonfrise74 (Vicar) on Nov 09, 2009 at 21:48 UTC
    Looking at this code, you want to change it to something like this.
    #!/usr/bin/perl use strict; my @record = ( "662-5555", "+1 102 892-1314" ); for (@record) { print if /(^\d{3}-\d{4}$)/; }
      Why did u put this array? my @record = ( "662-5555", "+1 102 892-1314" ); What if they are just individual values? Thanks, Pamela
        Did you want to run the "print if" against both values?