in reply to Re^2: pull single value
in thread pull single value

Did you want to run the "print if" against both values?

Replies are listed 'Best First'.
Re^4: pull single value
by Anonymous Monk on Nov 10, 2009 at 00:20 UTC
    Actually this code will fetch attribute having multiple values, i want only a single value that matches my pattern. The code above is not working. I even tried this one.
    #!/usr/bin/perl use strict; use warnings; my @array; my $record = "+1 102 892-1314"; $record = "662-5555"; if ($record =~ m/(^\d{3}-\d{4}$)/) { push @array, $record; } print "@array";
      It's rather useless to put a value in a variable when you immediately replace it with another.
Re^4: pull single value
by Anonymous Monk on Nov 10, 2009 at 00:28 UTC
    yes.
Re^4: pull single value
by Anonymous Monk on Nov 10, 2009 at 00:47 UTC
    How to do that?
      Then you're not dealing with a single value as you claimed. You want to iterate over a list of values. Why do you think it's a problem storing that list in an array?

      As for how, you've already been shown how. You were even given a fully functional program.

        Thanks for all your efforts. Let's simplify this. I am trying to put this in a subroutine. But it is not working. can you suggest on this?
        sub OfficePhoneNumber { my @numbers = @_; my $number; my @results; foreach $number (@numbers) { if ($number =~ m/(^\d{3}-\d{4}$)/) { push @results, $number; } } return @results; }
        &OfficePhoneNumber "554-3214,+1 302-342-2345";