Professional Web Applications Themes

global matching - PERL Beginners

Hi, Could you help me to solve this problem? I have this file: foobar-45whatever-37hello16goodbye9#!!! snafu23skidoo---+++++30++++-50 I need to store all digits into list. As you can see there is no any obvious delimiter, so I'm using global matching open (INFILE, "$ARGV[0]") || die "cannot open $ARGV[0].\n"; digits = <INFILE> =~ /-*?\d+/g; print "digits"; The problem is that digits took only first line of my file. Thanks -Vladimir...

  1. #1

    Default global matching

    Hi,

    Could you help me to solve this problem?



    I have this file:

    foobar-45whatever-37hello16goodbye9#!!!

    snafu23skidoo---+++++30++++-50



    I need to store all digits into list. As you can see there is no any obvious delimiter, so I'm using global matching



    open (INFILE, "$ARGV[0]") || die "cannot open $ARGV[0].\n";
    digits = <INFILE> =~ /-*?\d+/g;
    print "digits";



    The problem is that digits took only first line of my file.



    Thanks

    -Vladimir



    Vladimir Guest

  2. #2

    Default RE: global matching

    Vladimir Lemberg wrote: 
    change to a while loop:
    while ( <INFILE> ) {
    digits = /-*?\d+/g;
    print "digits";
    }
    Wags ;) 



    ************************************************** *****
    This message contains information that is confidential
    and proprietary to FedEx Freight or its affiliates.
    It is intended only for the recipient named and for
    the express purpose(s) described therein.
    Any other use is prohibited.
    ************************************************** *****

    Wagner, Guest

  3. #3

    Default Re: global matching

    Vladimir Lemberg wrote: 

    You can enable "slurp mode":

    local $/;
    my digits = <INFILE> =~ /-?\d+/g;

    (as far as I understand, the * quantifier is redundant)

    Or you can see to it that all the lines are processed:

    my digits;
    push digits, /-?\d+/g for <INFILE>;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  4. #4

    Default Re: global matching

     


    You read only 1 line... for more, you may need,

    my $lines = join "", <INFILE> ;
    digits = $lines =~ /-*?\d+/g;

    or

    my digits ;
    while (<INFILE>)
    { my get = $_ =~ /-*?\d+/g;
    push digits, get
    } print digits


    HTH,
    Bee

    Bee Guest

Similar Threads

  1. matching
    By Eric Walker in forum PERL Beginners
    Replies: 5
    Last Post: November 18th, 04:37 PM
  2. matching ??
    By Tim in forum PERL Miscellaneous
    Replies: 5
    Last Post: October 25th, 09:49 AM
  3. Replies: 1
    Last Post: July 29th, 03:12 AM
  4. Replies: 0
    Last Post: July 17th, 09:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139