Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

generating a sequence of "namenumber" in Perl or PHP

Posted on 2008-06-24
7
Medium Priority
?
238 Views
Last Modified: 2008-06-24
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
0
Comment
Question by:lucavilla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 21860580
Could go like this:
$start =  8;
$end   = 12;
$fp = fopen('newfile.txt', 'w');
foreach (file('wordsperline.txt') as $line) {
   for ($i = $start; $i<= $end; ++$i)
     fputs($fp, trim($line) . $i ."\n");
}
fclose($fp=;

Open in new window

0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 800 total points
ID: 21860618
Call like:
    script.pl file.txt

#!/usr/bin/perl
use strict;
use warnings;
 
while(<>) {
	chomp;
	for my $i (8..12) {
		print "$_$i\n";
	}
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 21860753
perl -lne 'print for <${_}{8,9,10,11,12}>' file
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:lucavilla
ID: 21860948
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.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21861140
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.
0
 
LVL 84

Accepted Solution

by:
ozo earned 1200 total points
ID: 21861256
dos shell handles quotes differently
C:\Perl\bin\perl -lne "print for <${_}{8,9,10,11,12}>" file.txt
0
 

Author Comment

by:lucavilla
ID: 21861552
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?  :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question