Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

serial number generator

by royserna03 (Initiate)
on Oct 03, 2012 at 00:42 UTC ( #996942=perlquestion: print w/ replies, xml ) Need Help??
royserna03 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I am trying to make a serial number generator with the user's input. My problem with this code is that when I want the array to be on the excel file, it just gives me the last object of the array. However if I use print, it prints all of them. Also I cannot find a way to keep a counter so that after the user is done, the numbers won't repeat. Any help will be very appreciated. Thank You

#!/usr/bin/perl -w use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new( 'simple.xlsx' ); my $worksheet = $workbook->add_worksheet(); print "How many Sirial Numbers do you need?\n"; $comp= ITS; $mal= <STDIN>; for ($number=0001; $number< $mal; $number++) { $exp= $comp.$number; } @array= $exp; $array_ref = \@array; $worksheet->write_col( 0,0 , $array_ref);

Comment on serial number generator
Download Code
Re: serial number generator
by Athanasius (Monsignor) on Oct 03, 2012 at 03:03 UTC

    Hello royserna03, and welcome to the Monastery!

    The following script should do what you want:

    #!/usr/bin/perl use strict; use warnings; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new( 'simple.xlsx' ); my $worksheet = $workbook->add_worksheet(); print "How many Serial Numbers do you need?\n"; my $mal = <STDIN>; my $comp = 'ITS'; my @array; for (my $number = 1; $number <= $mal; $number++) { my $exp = $comp . sprintf("%04d", $number); push @array, $exp; } $worksheet->write_col(0, 0, \@array);

    Some notes:

    • Always use strict; and declare lexical variables with my.

    • Barewords must be quoted: my $comp = 'ITS';

    • If you want (say) 5 serial numbers, and they start at 1, then the last number must be 5, so the loop condition needs to be $number <= $mal.

    • You can use sprintf to prepend 0s to the serial number.

    • The key point: Each time a new value of $exp is calculated, it must be added to the array. See push.

    Hope that helps,

    Athanasius <°(((><contra mundum

      The fence picket error is avoided by avoiding the C for loop. Use a Perl for loop instead and a range:

      for my $number (1 .. $mal) { ... }

      which is clearer, more concise and correct.

      True laziness is hard work

      Thank You so much! that was a really big help, I really appreciate the help! I just have a few more questions if that's ok. How can I store the last number create, so that after the usr closes the program it picks it right back from where it ended. Ex. last number is ITS0012, usr closes and opens again and the first number that the usr gets is ITS0013. Also how can I have two spread sheets, one with the recently made numbers and another one with all the numbers that have been created? Again I want to thank you for such a fast and awesome response! I was so stuck, I really appreciate the support!

        I think this will do what you want:

        #!/usr/bin/perl use strict; use warnings; use Excel::Writer::XLSX; use Spreadsheet::XLSX; my $master = 'master.xlsx'; -f $master or die "Spreadsheet '$master' not found: $!"; my $excel = Spreadsheet::XLSX->new($master) or die "Cannot open spreadsheet '$master' for reading: $!"; my $sheet = $excel->{Worksheet}[0]; my $last_old_id = $sheet->{Cells}[ $sheet->{MaxRow} ][0]{Val} // 'IT +S0000'; $last_old_id =~ /(\d+)/; my $first_new_id = $1 + 1; print "How many Serial Numbers do you need?\n"; my $mal = <STDIN>; my $comp = 'ITS'; my @array; for my $number (1 .. ($first_new_id + $mal - 1)) { my $exp = $comp . sprintf("%04d", $number); push @array, $exp; } my $simple = 'simple.xlsx'; my $workbook = Excel::Writer::XLSX->new($simple) or die "Cannot open spreadsheet '$simple' for writing: $!"; my $worksheet = $workbook->add_worksheet(); my @array2 = @array[($first_new_id - 1) .. ($first_new_id + $ma +l - 2)]; $worksheet->write_col(0, 0, \@array2); $workbook = Excel::Writer::XLSX->new($master) or die "Cannot open spreadsheet '$master' for writing: $!"; $worksheet = $workbook->add_worksheet(); $worksheet->write_col(0, 0, \@array);

        The full set of IDs is stored in the first column of master.xlsx. The new IDs are (over-)written into the file simple.xlsx, as before.

        Hope that helps,

        Update: As Anonymous Monk implies below, you have to create an Excel file called master.xlsx first (using Excel), before you run the code. The file should initially be empty.

        Athanasius <°(((><contra mundum

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996942]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-07-29 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (224 votes), past polls