in reply to pull single value

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;

Replies are listed 'Best First'.
Re^2: pull single value
by Anonymous Monk on Nov 09, 2009 at 21:04 UTC
    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.