?
Solved

Need explanation of some perl syntax

Posted on 2007-08-03
6
Medium Priority
?
240 Views
Last Modified: 2013-11-05
Please explain each line character by character.  What is the purpose is chomp here?

while (<>) {
    chomp;
    if (/^\s*0x([0-9a-fA-F]+)\s*__AID_LOC/) {
        $aid_loc = hex($1);

# more perl statements

}
0
Comment
Question by:naseeam
  • 3
  • 2
6 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 960 total points
ID: 19626221
while (<>) {   <<< reads from standart input, line by line
    chomp;      <<< removes the End Of Line character from the line.
0
 
LVL 1

Author Comment

by:naseeam
ID: 19626339
Thanks.

I requested explanation of each line.

WHAT IS BEING READ FROM STANDARD INPUT?
Please explain if condition.  What's all that obscure syntax in the if condition?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 960 total points
ID: 19626375
>WHAT IS BEING READ FROM STANDARD INPUT?
well, what the user puts in, resp is redirected into the standard input.


>    if (/^\s*0x([0-9a-fA-F]+)\s*__AID_LOC/) {
that checks, with the regular expression of ^\s*0x([0-9a-fA-F]+)\s*__AID_LOC (which means in short of the input is starting with 0-0 or a-f or A-F ),

> $aid_loc = hex($1);
and puts the match into the variable $aid_loc

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:naseeam
ID: 19626539
Could you provide a detailed example.  Enter some input data and manually execute above code to come up with match that would get put into $aid_loc.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 960 total points
ID: 19626608
i have to admit that I am rather a newbie on Perl, and most I know about perl is from similarities from other languages I know much better.
I would hence suggest you get through some tutorials, for example:
http://www.english.uga.edu/humcomp/perl/regex2a.html
and some more detailed explanations:
http://www.english.uga.edu/humcomp/perl/regular_expressions.HTML

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 1040 total points
ID: 19626930
The regular expression is looking for a hex number, followed optionally by whitespace (space, tab) then the literal string "__AID_LOC", saving the hex number to the special variable $1.  It is then using the hex function to convert the hex number to a number, and storing the result in $aid_loc.

So, if  your input was:     "0xabcd __AID_LOC"
Then the $aid_loc variable will contain 43981.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
Six Sigma Control Plans
Suggested Courses

840 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