CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Splitting a scalar

 



Troy
Novice

May 23, 2002, 2:49 AM

Post #1 of 2 (633 views)
Splitting a scalar Can't Post

Hey all again, I probably didn't explain exactly what I am trying to do in my previous post so I'll try again:

I have a scalar which I want to split into an array eg:
$number = 3a5629bff667

Now I want each element of the new array to contain portions of the original scalar. ie the first three characters in array[0], the next three characters in array[1], etc as shown below

I want to end up with this:
@array[0] = 3a5
@array[1] = 629
@array[2] = bff
@array[3] = 667
etc

Thanks again,
- Troy


mhx
Enthusiast / Moderator

May 22, 2002, 9:58 PM

Post #2 of 2 (631 views)
Re: [Troy] Splitting a scalar [In reply to] Can't Post

So, that's even easier:


Code
$number = '3a5629bff667'; 
@array = $number =~ /.{1,3}/g;


Here's how it works:


Code
@array = $number =~ /.{1,3}/g;


The string stored in $number is bound to a regex that will match between one and three (but as much as possible) arbitrary characters. Since this is a global match in list context, all matched substrings are returned and stored in the @array.

Despite being off topic, I hope you learned a bit about regexes from the other thread, too. Wink

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives