LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-17-2002, 04:24 PM   #1
Danny.au
LQ Newbie
 
Registered: Jul 2002
Posts: 3

Rep: Reputation: 0
Simple Perl Question


Hi
I have an simple perl Question.
I't about the tr function.

I'm writing a perl scritp with some database function for a car-chop

My problem is that we are using ID-numbers for all cars and sometime I need to modify these ID-numbers

Lets say I have something like this

$car =~ tr /1/1-out/;

up to 60 like
$car =~ tr /60/60-in/;

Replacing 1 with 1-out is no problem at all.
But when it comes to replacing eg 22 with 22-in, this is the tricky part.

The program do not read it as 22, it thinks it's 2 AND 2 .

How can I fix so the program read it as 22 and not like the number 2 twice


Thanks
 
Old 07-18-2002, 07:00 AM   #2
te_conway
Member
 
Registered: Apr 2001
Location: MA
Distribution: redhat 7.2
Posts: 182

Rep: Reputation: 30
If your just changing out to in use substitute

$car =~ s /out/in/;
 
Old 07-18-2002, 08:04 AM   #3
Danny.au
LQ Newbie
 
Registered: Jul 2002
Posts: 3

Original Poster
Rep: Reputation: 0
Same problem with =~ s

the script looks like this

$car =~ s /1/1-in/;
$car =~ s /2/1-in/;
.
...and until 60 is reached
.
$car =~ s/59/59-in/;
$car =~ s/60/59-in/;

but if the user input is eg 10, it will not read the input as 10, it will read it as 1 and 0 .

And then replace it with '1-in' and '0-in' and not as '10-in'

I want it to 10-in , anyone know how to fix this ??
 
Old 07-18-2002, 08:32 AM   #4
te_conway
Member
 
Registered: Apr 2001
Location: MA
Distribution: redhat 7.2
Posts: 182

Rep: Reputation: 30
Sorry but I'll need an exact example. The code below works fine.

$car =~ s /22/22-out/;

Can you be more specific?
 
Old 07-18-2002, 08:55 AM   #5
Danny.au
LQ Newbie
 
Registered: Jul 2002
Posts: 3

Original Poster
Rep: Reputation: 0
Hi
Yes , sure.
Try this script and you will know what I mean

---------------------------------------------------

#!/bin/perl

print "Enter a number between 1 and 60 : ";

$car = <STDIN>;
chomp($car);

$car =~ s /1/1-in/;
$car =~ s /2/2-in/;
$car =~ s /3/3-in/;
$car =~ s /4/4-in/;
$car =~ s /5/5-in/;
$car =~ s /6/6-in/;
$car =~ s /7/7-in/;
$car =~ s /8/8-in/;
$car =~ s /9/9-in/;
$car =~ s /10/10-in/;
$car =~ s /11/11-in/;
$car =~ s /12/12-in/;

print "\n$car\n";

-----------------------------------------------------


Run the script and then answer eg 1 3 6.

The result will be 1-in 3-in and 6-in.

But If i answer 11 and 12 the result will be : 1-in1 12-in-in

It seems like it converts 11 to 1-in and then the value 1.

I want the result of 11 and 12 to be : 11-in and 12-in
 
Old 07-18-2002, 08:59 AM   #6
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
Well that would get kind of tricky. It would depend on whether there are any special characters before and after the number. The easiest solution would be to store a 1 as 01 instead. And then check for substituting 01.

If that's not an option then you will have to do maybe something like this:

sed -e 's/\([^0-9]\)1\([^0-9]\)/\11-in\2/g'

This will only work if there are other characters before and after the number (even if they are just spaces).
 
Old 07-18-2002, 09:46 AM   #7
te_conway
Member
 
Registered: Apr 2001
Location: MA
Distribution: redhat 7.2
Posts: 182

Rep: Reputation: 30
Try this

$car = <STDIN>;
chomp($car);

$car =~ s/(\d+)/$1-in/;

print "\n$car\n";

it assumes you're entering 1 car id at a time.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple Perl Question whohasit Programming 3 11-05-2004 10:52 AM
simple Perl question true_atlantis Programming 5 11-18-2003 08:37 PM
Simple Perl Question jacksmash Programming 1 11-13-2003 09:17 PM
Simple Perl question! Please help! jacksmash Programming 4 11-07-2003 05:56 AM
A simple perl programming question J_Szucs Programming 12 07-14-2002 10:03 PM


All times are GMT -5. The time now is 05:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration