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:
Sequence Number

 



jayasree
New User

Aug 27, 2013, 11:07 PM

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

I want to give sequence number in xml files,

For example
1.xml--><id>00000001></id>
2.xml--><id>00000002></id>

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"'


Laurent_R
Veteran / Moderator

Aug 27, 2013, 11:31 PM

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

Use the printf or sprintf functions.


jayasree
New User

Aug 27, 2013, 11:34 PM

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

In which case? i cant understand


jayasree
New User

Aug 27, 2013, 11:38 PM

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

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


jayasree
New User

Aug 28, 2013, 4:01 AM

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

Hi,

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;
}
else
{
for (my $ik=0; $ik<=$#FileArr; $ik++)
{
open(file3,"Process.out");
open(writefile,">>$Process1.out");

while ($sLine = <file3>) {


$sLine=~s/<id>(.*?)<\/id>/<id>$newcnt<\/id>/ig;

$newcnt=$newcnt++;

print file3;
}
}
}
close(file3);


The following error occured while executing the code.

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


FishMonger
Veteran / Moderator

Aug 28, 2013, 7:01 AM

Post #6 of 6 (804 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