Hello, monks! I am starting my third week of Perl experience. Coming from a (limited) C++ background, I am fascinated with the ease and potency of regular expressions. But now I have run into something I don't think regexes can solve.
The problem: I have a number of strings composed of a hodgepodge of information - dates, times, comments, other numbers, etc. I am trying to replace some of the dates with their epoch timestamps.
Thus far:
- I can find the dates I need in the strings (with m/ /)
- I can get the date converted to its epoch timestamp
Where I'm stuck: I'm not sure how to get the timestamp back into the appropriate string in the proper place(s). I have tried using a subroutine with search and replace, i.e. s/<regex to find date>/&<subroutine to calculate date's timestamp>/ge, but I continue to get syntax errors (I assume because you can't call subroutines inside a regex).
Thoughts on how to proceed:
- This would be easily solved if I could find essentially a "search, modify, replace" function or something similar. I'm only assuming one cannot call subroutines inside s/ / / because I am getting errors trying to do so - if it is indeed possible, please correct me - and maybe provide a dummy example too please. ^_^
- I guess I could also completely rebuild the strings, i.e. breakup every field (into an array), modify the ones I need to, and then reassemble the string. It just seems tedious and messy. I wanted to check with some more experienced folks to make sure I wasn't doing a lot of silly and unnecessary work.
Thanks!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.