adding letters to front and numbers to back of wordlist,bash perl or awk
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
adding letters to front and numbers to back of wordlist,bash perl or awk
i would love some help guys, using the following i need upper and lower case letters in the front of the word and 0000-9999 at the end. i did experiment a little but just keep getting errors
Notice that since we're using 'ABCabc' as the letters at the end, and we have 8 elements, we cycle back to A and B.
Here's my solution:
Code:
#! /usr/bin/perl
use warnings;
use strict;
my @numbers=( 0000 .. 9999 );
my @letters=( 'A' .. 'C', 'a' .. 'c' );
my $counter=0;
while( <> ) {
my $numcounter = $counter % scalar @numbers;
my $lettercounter = $counter % scalar @letters;
chomp;
printf "%04d%s%s\n", $numbers[$numcounter], $_, $letters[$lettercounter] ;
$counter++;
}
This will work with files and input piped into stdin. Also, your use of 'while ($line = <>) ...' is better style than my code using $_. No big deal for quick scripts, but it makes a difference when the script gets big.
Notice that since we're using 'ABCabc' as the letters at the end, and we have 8 elements, we cycle back to A and B.
Here's my solution:
Code:
#! /usr/bin/perl
use warnings;
use strict;
my @numbers=( 0000 .. 9999 );
my @letters=( 'A' .. 'C', 'a' .. 'c' );
my $counter=0;
while( <> ) {
my $numcounter = $counter % scalar @numbers;
my $lettercounter = $counter % scalar @letters;
chomp;
printf "%04d%s%s\n", $numbers[$numcounter], $_, $letters[$lettercounter] ;
$counter++;
}
This will work with files and input piped into stdin. Also, your use of 'while ($line = <>) ...' is better style than my code using $_. No big deal for quick scripts, but it makes a difference when the script gets big.
close, more like
AFoo0000
BBar0001
CBaz0002
thank you very much for the extended script advice! that's the direction its heading in many parts lol. why the 'A' quotes around the A?
thank you very much for the extended script advice! that's the direction its heading in many parts lol. why the 'A' quotes around the A?
That's because I'm using the 'use strict' directive (and so should you ;-) ).
'use strict' enforces a number of syntax rules that have to do with how variables and constants are named and/or used.
The two things that it typically catches are variables which are not defined with the 'my' or 'our' keywords, and the use of 'barewords', i.e. words which are not variables or keywords which are not quoted.
If I had not put quotes around the string constants, I would have gotten the following compile errors:
Code:
Bareword "C" not allowed while "strict subs" in use at /tmp/test.pl line 6.
Bareword "A" not allowed while "strict subs" in use at /tmp/test.pl line 6.
Bareword "c" not allowed while "strict subs" in use at /tmp/test.pl line 6.
Bareword "a" not allowed while "strict subs" in use at /tmp/test.pl line 6.
Execution of /tmp/test.pl aborted due to compilation errors.
Using the 'strict' directive will save you all kinds of frustration from mis-spelled variables and having the perl interpret barewords in bizarre ways... and while you're at it, always use the warnings directive as well, and fix any warnings emitted. The other way leads to insanity.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.