in reply to Re^2: Slurping file using regular expressions for array or variables
in thread Slurping file using regular expressions for array or variables

As a wise man once said, 'you can't just make s**t up, and expect the computer to understand.'

Your three dots between the captures means the second capture will fail unless there are exactly 3 anythings (characters between the first and the second item you're trying to capture.

Please, do as others have suggested: use perldoc to read the basic regex docs ( perlrequick, perlretut and perlredoc ) until you truly understand at least those elements you're trying to use.


Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
  1. code
  2. verbatim error and/or warning messages
  3. a coherent explanation of what "doesn't work actually means.
  • Comment on Re^3: Slurping file using regular expressions for array or variables

Replies are listed 'Best First'.
Re^4: Slurping file using regular expressions for array or variables
by firefli (Initiate) on Apr 04, 2014 at 20:34 UTC

    Ok, I shouldn't have put dots there - I wasn't trying to use it as a perl wildcard, more like a "etc" which I did not make clear. I had used .* but only got one mac address and one ipaddress. The question isn't related to what goes between the captures but more about whether the two captures can be sent to the arrays. If you have a link for me to read about that, I would be grateful. I don't think writing about making stuff up is useful for feedback although I understand that I should have not use the three dots and used something else to indicate that that wasn't what I was concerned about in that particular post.

      The code below addreses itself solely to your question about how " the two captures can be sent to the arrays.." It is inappropriate and inadqequate for production use in a variety of ways:
      1. It offers no illustration of how to process multiple lines of your source data
      2. it makes no attempt to ensure that your customer can identify a particular IP with its associated MAC address (particularly when the source data is borked)
                and, very important,
      3. its design is (intentionally) flawed, in that it has no plan to deal with incomplete data, failed matches, and the like, or numerous other problems which may challenge you
      my $text='abc-123'; if ( $text =~ /(abc)-(\d{3})/) { say "\$1 is $1"; # rather than printing the matches, as this doe +s, say "\$2 is $2"; # you could push the matches from each iteratio +n # over the full text to two arrays } # i.e., something like... push @IPs, $1; push @MACs, $2; }

      Almost all (good) introductions to Perl will teach you how to use push very early in the game (again, read the docs!). And had you taken the advice to read the docs on regexen, the code below should have been on the tip of your tongue (or fingertips).

      I may be wrong, of course, but you seem to me to be trying to skip over the sometimes difficult but essential task of getting the basics down pat... so I say again, read the documents. This site has many very fine tutorials; so too, do may colleges and universities (and often those are accessible online at no charge). And if investing in a book is within your means, run -- don't walk -- to your nearest good bookstore and buy a copy of learning Perl.


      Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
      1. code
      2. verbatim error and/or warning messages
      3. a coherent explanation of what "doesn't work actually means.