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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Sequence Number


New User

Aug 27, 2013, 11:07 PM

Post #1 of 6 (2846 views)
Sequence Number Can't Post

I want to give sequence number in xml files,

For example

I'm using the below code for single digit numbers, but i want to be 8 digit number format like above mentioned in id tags.
Could you please clarify this?

use warnings;
use strict;

my @lst_array = qw (1 2 3 4 5 6 7 8 11 12 13 14);
my $start = $lst_array[0];

for (my $i=1; $i<@lst_array; $i++){
if (($lst_array[$i] - $lst_array[$i -1]) != 1 ){
print "$start - $lst_array[$i-1], ";
$start = $lst_array[$i];
print "$start -$lst_array[-1]\n"'

Veteran / Moderator

Aug 27, 2013, 11:31 PM

Post #2 of 6 (2843 views)
Re: [jayasree] Sequence Number [In reply to] Can't Post

Use the printf or sprintf functions.

New User

Aug 27, 2013, 11:34 PM

Post #3 of 6 (2842 views)
Re: [jayasree] Sequence Number [In reply to] Can't Post

In which case? i cant understand

New User

Aug 27, 2013, 11:38 PM

Post #4 of 6 (2841 views)
Re: [jayasree] Sequence Number [In reply to] Can't Post

$outfile = sprintf("%08d", $number); is this correct?

New User

Aug 28, 2013, 4:01 AM

Post #5 of 6 (2830 views)
Re: [jayasree] Sequence Number [In reply to] Can't Post


I have tried the below format for global change for id, but its not working,

use warnings;
use strict;
my $newcnt=1;
my $sLine;
my $file3;
my $Process1;
my @FileArr = glob("*.xml");
if( !@FileArr )
print "\nNo files found.......\n";
sleep 10;
for (my $ik=0; $ik<=$#FileArr; $ik++)

while ($sLine = <file3>) {



print file3;

The following error occured while executing the code.

Unquoted string "writefile" may clash with future reserved word at line 21
Name "main::writefile" used only once: possible typo at line 21.
Use of uninitialized value $Process1 in concatenation (.) or string at lin
e 21.
readline() on closed filehandle file3 at line 23.

Veteran / Moderator

Aug 28, 2013, 7:01 AM

Post #6 of 6 (2825 views)
Re: [jayasree] Sequence Number [In reply to] Can't Post

The first problem is that you're manually parsing an xml file with a regex instead of using an XML parser. If the xml data is simple enough, then a simplistic regex approach can work, but is a very fragile approach.

Why are you opening "Process.out" in a loop? That doesn't make any sense.

$Process1 is declared, but never assigned a value, so attempting to open a filehande to "$Process1.out" isn't going to be very fruitful.

You should ALWAYS check the return code of an open call to make sure it was successful and take action if it wasn't. You can do that test explicitly on each open call, or you can use the autodie pragma which will do that check for you.

When opening a filehandle, you should use a lexical var for the handle instead of a bareword. The bareword writefile that you used for the handle is what the first error message is referring to. If you had written that in uppercase, perl would not have complained but you should use a lexical var instead.

Don't use the C style for loop syntax. It's cleaner and more efficient to use perl's for loop syntax.

Do you want to use the existing id numbers and just format them with leading zeros, or do you want as is implied in your code to replace them with a new sequential (formatted) number even if it's not the same value as the original?


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

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