Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: More Help with Regex

by chipmunk (Parson)
on Jan 31, 2002 at 15:23 UTC ( #142428=note: print w/replies, xml ) Need Help??

in reply to More Help with Regex

In order to understand what is happening, you need to know how split works. split is used to split a string into fields based on a separator character or characters. For example, split(/,/, "Bob,17,M") returns the list ('Bob', '17', 'M') because the commas are treated as separators, and the text between them as fields. When the regex includes capturing parens, the captured separators are returned along with the fields, so split(/(,)/, "Bob,17,M") returns ('Bob', ',', '17', ',', 'M').

split /(..)/, '00a0c801adc6') treats each pair of characters as a separator, and the text between each pair of characters as a field. Of course, there is no text between the characters, so you get null strings for the fields: ('', '00', '', 'a0', '', 'c8', '', '01', '', 'ad', '', 'c6'). (The trailing empty fields are discarded.) When you join that list back together, you get extra colons because of all the null strings.

When you want to grab groups of characters from a string without separators, instead of split you can use a plain old regex match with /g: print join(':', '00a0c801ad' =~ /../g), "\n<BR>";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://142428]
[Lady_Aleena]: Discipulus, then that is a problem. I wanted to find total seconds of my entire .mp3 collection to do some math on it to see how many days of continuous music i have.
[LanX]: darn. .. I wanted to see Marine and Melonchon go to next round, just for fun xD
[Discipulus]: LA use Perl: traversing a dir tree is simple; foreach file mp3 $sec+= use Mp3::Info
[Lady_Aleena]: Discipulus, I was able to get the sum of the seconds on the command line using awk. Then I ran the result through a perl script and got the result. It came bach with 4 days, 9 hours, 56 minutes, 43 seconds.
[Lady_Aleena]: s/bach/back/;
[Discipulus]: gired anyway is the intermediate state between hired and fired (and vice versa;)
[Lady_Aleena]: Discipuluis, I don't know if I want to fight with File::Find. File::Find and I are not friends.
[Discipulus]: 4 day Bach is too muche even for me
[Discipulus]: LA you know who is paco?
[Lady_Aleena]: Disc, nope.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2017-04-23 20:32 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.