Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Regular expression

by Athanasius (Monsignor)
on Oct 30, 2012 at 06:13 UTC ( #1001460=note: print w/ replies, xml ) Need Help??


in reply to Regular expression

Parsing from right to left:

  • Assuming KEY_FH is a filehandle which has been opened for reading, grep places <KEY_FH> into list context, so it returns a list of the lines in the input file.
  • grep applies the regex !/^$/ to each element, filtering out blank lines. So, the output of the call to grep is a list of the non-blank lines in the input file.
  • map takes these lines as input, and applies the split function to each.
  • This splits on the pipe character (“|”), optionally surrounded by whitespace.
  • The LIMIT of 2 ensures that split will return at most 2 fields. So if the line contains more than one |, only the first will be used to split the line into fields. Assuming the line contains at least one |, split will return a list of two fields.
  • These two fields are assigned to the hash %key_hash, where they form a key-value pair.
  • Finally, chop is applied to the whole hash, which removes the final character from the value half of each key-value pair. This will presumably be the newline character read in at the end of each line. (But chomp would be a better choice here.)

Hope that helps,

Athanasius <°(((><contra mundum


Comment on Re: Regular expression

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1001460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2014-10-21 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (110 votes), past polls