Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

explain code please

Posted on 2013-11-23
1
Medium Priority
?
197 Views
Last Modified: 2013-11-26
Could someone explain step by step what this code does please ?

/^2,[^,]*,[^,]*,[^,]*,[^,]*,(\d+)/ &&  $c{$1}++ while <M>;
0
Comment
Question by:Europa MacDonald
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 39672077
This is a shorthand way of writing

while (<M>) {
   if (/^2,[^,]*,[^,]*,[^,]*,[^,]*,(\d+)/) {
      $c{$1}++
   }
}

Open in new window


To break it down, the first bit is the regex

/^2,[^,]*,[^,]*,[^,]*,[^,]*,(\d+)/

This means any line (read from the file referenced by filehandle 'M') that

starts with 2  ^2
followed by zero or one character that isn't a comma  [^,]*
followed by a comma ,
...
the last part captures one or more digits (\d+)

This will match things like:

2,3,4,5,6,7
2,,,,,7
2,,4,5,6,7
etc

The last digit captured by the (\d+), which in the above examples is 7, is then used to increment the hash c, so effectively

$c{7}++

The hash is most likely used to count the total number of digits that are matched in the file.

The while <M> is simply a while loop using whatever filename is opened and assigned to the M filehandle.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question