473,394 Members | 1,696 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,394 software developers and data experts.

Regex and array issue...

Hi all,

I'm trying to take a line like this:
Expand|Select|Wrap|Line Numbers
  1. <guid isPermaLink="false">Entry 29</guid> 
Strip it of everything but the number, push that number into an array of other numbers, add one to the highest value, and print that value.

Expand|Select|Wrap|Line Numbers
  1. if ($ngrab =~ /<guid.*/)
  2. {
  3.   $ngrab =~ s/<guid.*Entry.//i; $ngrab =~ s/<\/guid>//i;
  4.   push(@guid, $ngrab); 
  5. }
  6.  
  7. $i = scalar(@guid); $i++;
  8. @sguid = sort(@guid);
  9. print $sguid[$i];
  10.  
However, I'm not getting anything. Is the way I'm going about this fundamentally wrong or is there some small bit to change to get it working?

Thanks for your help!
Sep 11 '07 #1
4 1590
numberwhun
3,509 Expert Mod 2GB
First, thank you for putting your code in code tags. :-) Just a hint. If you put code=perl inside the square brackets, it formats the code with color coding as well and looks really nice.

Second, I can start you in the right direction by at least helping you match the two numbers in the string. Here is the code I just whipped up:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. open(FILE, "<./data.txt");
  5.  
  6. while(<FILE>)
  7. {
  8.     if($_ =~ m/^.*(\d{2}).*$/ )
  9.     {
  10.         chomp($_);
  11.         print("$1\n");
  12.     }
  13.     else
  14.     {
  15.         print("Not Found.  Regex didn't work!\n");
  16.     }
  17. }
  18.  
  19. close(FILE);
  20.  
That will print out all of the numbers in the following data that I took from your example:

Expand|Select|Wrap|Line Numbers
  1. DATA FILE
  2.  
  3. <guid isPermaLink="false">Entry 29</guid>
  4. <guid isPermaLink="false">Entry 29</guid>
  5. <guid isPermaLink="false">Entry 29</guid>
  6. <guid isPermaLink="false">Entry 29</guid>
  7. <guid isPermaLink="false">Entry 29</guid>
  8. <guid isPermaLink="false">Entry 29</guid>
  9. <guid isPermaLink="false">Entry 29</guid>
  10. <guid isPermaLink="false">Entry 29</guid>
  11. <guid isPermaLink="false">Entry 29</guid>
  12. <guid isPermaLink="false">Entry 29</guid>
  13.  
Go ahead and use that regex and see if it helps get the rest of your code going.

BTW, is this homework? I am just curious as it sounds like an assignment.

Regards,

Jeff
Sep 11 '07 #2
KevinADC
4,059 Expert 2GB
Hi all,

I'm trying to take a line like this:
Expand|Select|Wrap|Line Numbers
  1. <guid isPermaLink="false">Entry 29</guid> 
Strip it of everything but the number,
Expand|Select|Wrap|Line Numbers
  1. $ngrab = '<guid isPermaLink="false">Entry 29</guid>';
  2. $ngrab =~ tr/0-9//dc;
  3. print $ngrab;
Sep 11 '07 #3
Thanks all!

I'll give this stuff a shot and let you know.

@numberwhun: No, it's not homework. The library CMS where I work doesn't support blog software, and so I'm throwing together a script to help the librarians and assistants update the RSS feed without having to hack any code. I have everything working swimmingly except that one thing. Regex is not one of my strong-suits. :-(

Update: Thanks in large part to your suggestions, I got this working. However, I did so by ditching the array all-together. It occurred to me that there was no real reason to use it, because I only needed the highest value, which would come first. Here was my solution:

Expand|Select|Wrap|Line Numbers
  1. if (($ngrab =~ /<guid.*/) && ($grab != 1))
  2. {
  3.   $ngrab =~ tr/0-9//dc;      # Much more elegant than my method!  Thanks!
  4.   chomp($ngrab);
  5.   $check = $ngrab;
  6.   $grab = 1;
  7. }
  8.  
  9. $check++;
  10.  
  11. print $check;
  12.  
Sep 12 '07 #4
numberwhun
3,509 Expert Mod 2GB
Thanks all!

@numberwhun: No, it's not homework. The library CMS where I work doesn't support blog software, and so I'm throwing together a script to help the librarians and assistants update the RSS feed without having to hack any code. I have everything working swimmingly except that one thing. Regex is not one of my strong-suits. :-(
No worries, it was not an accusation. I was just curious as to the nature of what you were working on is all. It sounds like an interesting project.

As for regex's, they left me bewildered in the beginning. PM me your email address and I will send you a document that REALLY helped me understand them.

Regards,

Jeff
Sep 12 '07 #5

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

Similar topics

5
by: Markus Ernst | last post by:
Hello I have a regex problem, spent about 7 hours on this now, but I don't find the answer in the manual and googling, though I think this must have been discussed before. I try to simply...
3
by: Alan Pretre | last post by:
Can anyone help me figure out a regex pattern for the following input example: xxx:a=b,c=d,yyy:e=f,zzz:www:g=h,i=j,l=m I would want four matches from this: 1. xxx a=b,c=d 2. yyy e=f 3....
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
9
by: Bob Dankert | last post by:
I am trying to create a regular expression to split a string at each newline, unless the newline is within a set of quotes. Unfortunately, I have little experience with Regular Expressions and...
8
by: rjb | last post by:
Hi! Could somebody have a look and help me to optimize the code below. It may look like very bad way of coding, but this stuff is very, very new for me. I've included just few lines. Regex...
3
by: Luis Esteban Valencia | last post by:
hello quite a simple one if you understand regular expressions vbscript and ..net, probably quite hard if you don't i have a single line input which offers classic search functionality, so if...
4
by: Friday | last post by:
Being an Old L.A.M.P guy, I beg you to please excuse my ignorance of dot.net (and all things Windows, for that matter). As part of an experiment (to learn enough ASP/VB.net to port a series of ...
5
by: lgbjr | last post by:
Hello All, I have the following type of string: "X:Y\Z.exe" "123" What I need is an array of strings with the information from within each set of quotes. I was trying to use a Regex.Split, but...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
15
by: Kay Schluehr | last post by:
I have a list of strings ls = and want to create a regular expression sx from it, such that sx.match(s) yields a SRE_Match object when s starts with an s_i for one i in . There might be...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.