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;

In reply to Re: pull single value by ikegami
in thread pull single value by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.