Solved

explain code please

Posted on 2013-11-23
1
189 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:MichaelGlancy
1 Comment
 
LVL 48

Accepted Solution

by:
Tintin earned 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now