lucavilla
asked on
generating a sequence of "namenumber" in Perl or PHP
I have a file like this:
yellow
black
red
How can I obtain a new file that repeats those names followed by numbers of a specified range?
For example for 8 to 12 it must generate this:
yellow8
yellow9
yellow10
yellow11
yellow12
black8
black9
black10
black11
black12
red8
red9
red10
red11
red12
yellow
black
red
How can I obtain a new file that repeats those names followed by numbers of a specified range?
For example for 8 to 12 it must generate this:
yellow8
yellow9
yellow10
yellow11
yellow12
black8
black9
black10
black11
black12
red8
red9
red10
red11
red12
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
perl -lne 'print for <${_}{8,9,10,11,12}>' file
ASKER
Unfortunately I get errors with all the proposed solutions:
hernst42:
C:\PHP\php -r yourscript.scp
PHP Parse error: syntax error, unexpected $end in Command line code on line 1
Adam314:
C:\Perl\bin\perl yourscript.pl file.txt
Bareword found where operator expected at script.pl line 4, near ") as"
(Missing operator before as?)
Bareword found where operator expected at script.pl line 6, near ")
fputs"
(Missing operator before fputs?)
syntax error at script.pl line 4, near ") as "
syntax error at script.pl line 6, near ")
fputs"
Execution of script.pl aborted due to compilation errors.
ozo:
C:\Perl\bin\perl -lne 'print for <${_}{8,9,10,11,12}>' file.txt
The system cannot find the file specified.
The "file.txt" is in the current dir so I don't know why it doesn't find it. I suppose that the problem is in the syntax of the command.
hernst42:
C:\PHP\php -r yourscript.scp
PHP Parse error: syntax error, unexpected $end in Command line code on line 1
Adam314:
C:\Perl\bin\perl yourscript.pl file.txt
Bareword found where operator expected at script.pl line 4, near ") as"
(Missing operator before as?)
Bareword found where operator expected at script.pl line 6, near ")
fputs"
(Missing operator before fputs?)
syntax error at script.pl line 4, near ") as "
syntax error at script.pl line 6, near ")
fputs"
Execution of script.pl aborted due to compilation errors.
ozo:
C:\Perl\bin\perl -lne 'print for <${_}{8,9,10,11,12}>' file.txt
The system cannot find the file specified.
The "file.txt" is in the current dir so I don't know why it doesn't find it. I suppose that the problem is in the syntax of the command.
On my script, are you sure you copied/pasted correctly. The string that is causing an error ") as" doesn't exist in the script, and line 4 is blank. What version of perl are you using?
perl -v
For ozo's script on windows, you need double quotes instead of single quotes.
perl -v
For ozo's script on windows, you need double quotes instead of single quotes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
oopss.. Adam314, I used the hernst42's script for your test.. sorry.. your script worked, as the script of Ozo. Thanks both!
By the way Ozo, your minimalist solutions are always the best! You're really a genius, my favourite genius. I'm curious... where are you from? :)
By the way Ozo, your minimalist solutions are always the best! You're really a genius, my favourite genius. I'm curious... where are you from? :)
Open in new window