473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Populate array with split problem.

Kelicula
176 Expert 100+
Hello all. I have a problem which seems to make no sense to me.
Therefore I must be doing something wrong.

I am trying to populate an array using split with a regexe.
Here is the code (snippet only).
Expand|Select|Wrap|Line Numbers
  1. my $date = $q->param('date'); # 20090112 format
  2. @dateArray = split(/(\d{4})(\d{2})(\d{2})/, $date);
  3.  
After this I loop through the array using print and all the values show up.
BUT if I try to access a particular indice nothing shows up. ???

example:
Expand|Select|Wrap|Line Numbers
  1.  
  2. # After above code
  3. for(@dateArray){
  4. print $_, "\n";
  5. }
  6.  
  7. # This works printing 2009 \n 01 \n 12 \n
  8. # BUT....
  9.  
  10. print $dateArray[0]; # OR...
  11. print "$dateArray[0]";
  12.  
  13. # Prints nothing to the screen.
  14.  
What am I doing wrong?

I really need the three individual values, NOT the whole array.
Is there a better what to accomplish this without split?

I just need to pass a number, and be able to use the parts separately.

thanks in advance!
Apr 10 '09 #1
5 3005
KevinADC
4,059 Expert 2GB
Try this:

Expand|Select|Wrap|Line Numbers
  1. for my $i (0..$#dateArray) {
  2.    print "index $i = <$dateArray[$i]>\n";
  3. }
See what gets printed. The <> brackets are so you can see if there are empty elements in the array. Keep in mind you have used capturing parentheses in the regexp so the split delimiter is also being saved in the array.
Apr 10 '09 #2
Kelicula
176 Expert 100+
Very interesting.
Here is the results from your code KevinADC.
Expand|Select|Wrap|Line Numbers
  1. index 0 = <>
  2. index 1 = <2009>
  3. index 2 = <01>
  4. index 3 = <12>
  5.  
My only guess is the capturing vars, $1, $2, etc..were used as indices?

Thanks!

PS- I almost didn't notice your new avatar. Nice.
Apr 10 '09 #3
KevinADC
4,059 Expert 2GB
No, the capturing vars are not used as the array indexes. I think the problem is you are using split() when you should not be. Just use a matching regexp:

Expand|Select|Wrap|Line Numbers
  1. @dateArray = $date =~ m/(\d{4})(\d{2})(\d{2})/;
  2.  
if there is more than one set of dates to capture add the "g" modifier to the end of the regexp,
Apr 10 '09 #4
Kelicula
176 Expert 100+
Ah ha! That's it.
That works just as I expect it to.

In fact that is exactly what I was trying to do, I had seen it before in a book and remembered it incorrectly.

Thanks.
Apr 10 '09 #5
KevinADC
4,059 Expert 2GB
You're welcome, good to see you around here.
Apr 10 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve | last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not creating the new order, but getting the remaining...
11
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML....
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
2
by: Anil | last post by:
Hi All, I have a string which has product names, which are seperated by comma. The number of products in the string are random. I want to populate the array using the string, one product per...
3
by: Guy Bloomfield | last post by:
Does anyone know of an easy way to populate an array from a delimited text file when you don't know the number of columns ahead of time? I've been trying all day to use a combination of...
2
by: Thirsty Traveler | last post by:
I have an application that will be accessing an array of ports obtained from a config file. What would be the best way to do populate the array if the config file has, say, the following format: ...
11
by: doraima29 | last post by:
Hi, I am a newbie at PERL and really wanted to understand how server-side programming really works and operates since I use at the workplace. I use ASP and wanted to learn more about server-side...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.