473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Populate array with split problem.

Kelicula
176 Recognized Expert New Member
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 3015
KevinADC
4,059 Recognized Expert Specialist
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 Recognized Expert New Member
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 Recognized Expert Specialist
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 Recognized Expert New Member
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 Recognized Expert Specialist
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
3130
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 items from the original order cleaned up in the array. What I've tried to do so far is: 1) The data is stored in a serialized array in the order_data field in the orders table. When the order is selected, it is unserialized and called $order_data....
11
8115
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. -------------------- ---------- ---------- ------- ------- 21/03/2005-04:06:03 XX,XX XX,XX 171 3,42 21/03/2005-12:23:53 XX,XX XX,XX 500 5,4 21/03/2005-12:43:10 XX,XX XX,XX 553 5,38
12
55548
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 also removed) So far I have (val is value, ar is array, returns new array):
11
39453
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 rst.EOF i = i + 1 If rst!Something = Then astrMyArray(i) = rst!Something
4
8807
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
2
928
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 level. Please let me know how to do it. Thanks in Advance, Anil
3
1615
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 String.Split and the arraylist.add and the best I've managed to do is create an array of arrays. I searched Google and some people recommending using the Oledbprovider for Jet but that seems a bit hokey to me.
2
2111
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: <configuration> <applicationSettings> <AppURL server=http://DevAppServer> <port value="8000"/> <port value="8100"/> <port value="8200"/>
11
3198
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 programming since I am in the field of web design, development and multimedia. Here are some tips and need some help how to program: The way I learn is that I need an detailed explanation why and how we need use certain functions in PERL: I...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.