Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a translation program 3

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
Hello all,

I need a program to tranlate days of the week from one format to another. The translation need to work like this:

My program gets an input such as:

M needs to translate to nynnnnn (M is Monday)
Tu needs to translate to nnynnnn (Tu is Tuesday)
M,Tu needs to translate to nyynnnn (Monday and Tuesday)

In other words the translation needs to be in the form of 7 characters with a "y" representing the days that are passed to it and a "n" represents the days not passed to it. The first character is always Sunday so if the input is:

Su (Sunday) needs to translate to ynnnnnn.

There can be any number of days up to 7 (yyyyyyy) but there will always be at least one. I am messing with the long form of this:

Code:
my @schedule = split /,/, $schedule;
unless ($schedule[1]) {
	if ($schedule[0] =~ /M/) {
		$new_sched = "nynnnnn";
	}elsif ($schedule[0] =~ /Tu/) {
		$new_sched = "nnynnnn";
	}elsif ($schedule[0] =~ /W/) {
		$new_sched = "nnnynnn";
	}elsif ($schedule[0] =~ /Th/) {
		$new_sched = "nnnnynn";
	}elsif ($schedule[0] =~ /F/) {
		$new_sched = "nnnnnyn";
	}elsif ($schedule[0] =~ /Sa/) {
		$new_sched = "nnnnnny";
	}elsif ($schedule[0] =~ /Su/) {
		$new_sched = "ynnnnnn";
	}
}
unless ($schedule[2]) {

As you can see to cover all the possibilities this gets very long.

Any help is appriciated getting me going in the right direction.

Nick



If at first you don't succeed, don't try skydiving.
 
I think I may have found the solution. Handle each DAY seperately. Here is my code (untested)

Code:
	sub TransDays
	{
		# This subroutine translates the days format for the nhSchedule -list -full output
		# into the <ynnnnnn> format required for thr nhReport command
		my $new_sched = shift;
		if ($new_sched =~ /Su/) {
			$Sunday = "y";
		}else{
			$Sunday = "n";
		}
		if ($new_sched =~ /M/) {
			$Monday = "y";
		}else{
			$Monday = "n";
		}
		if ($new_sched =~ /Tu/) {
			$Tuesday = "y";
		}else{
			$Tuesday = "n";
		}
		if ($new_sched =~ /W/) {
			$Wednesday = "y";
		}else{
			$Wednesday = "n";
		}
		if ($new_sched =~ /Th/) {
			$Thursday = "y";
		}else{
			$Thursday = "n";
		}
		if ($new_sched =~ /F/) {
			$Friday = "y";
		}else{
			$Friday = "n";
		}
		if ($new_sched =~ /Sa/) {
			$Saturday = "y";
		}else{
			$Saturday = "n";
		}
	$trans_days = "$Sunday". "$Monday" . "$Tuesday" . "$Wednesday" . "$Thursday" . "$Friday" . "$Saturday";
	return $trans_days;	
	}

Any better ideas are appriciated.

Nick

If at first you don't succeed, don't try skydiving.
 
If you're deriving the day of the week from the localtime function, this would return a $wday result, which you could pass.

How're you deciding what days to pass?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks for the reply Paul. The days are passed to me from the output of a program command. The format is always:

M,Tu,W,Th,F,Sa,Su

So my code should work.

If at first you don't succeed, don't try skydiving.
 
I tested it and it works.

Don't I get a star??

[sadeyes]

If at first you don't succeed, don't try skydiving.
 
Did you guys miss me?
Just a little maybe?
Wanna try this for size then?

Code:
#!/usr/bin/perl -w
use strict;
my %indexes = (M=>0, Tu=>1, W=>2, Th=>3, F=>4, Sa=>5, Su=>6);

while(<>) {
  my $flags = "nnnnnnn";
  chomp;
  foreach my $day (split /,/, $_, -1) {
    substr($flags, $indexes{$day}, 1) = "y" if(exists($indexes{$day}));
  }
  print $flags, "\n";
}
You could, of course, easily make this into a function or remove embedded whitespace or do whatever you want.
I just thought I'd offer a solution that's just a few lines long rather than dozens.



Trojan.
 
A suggestion:

Code:
my %TransDays = (
   Su => 'ynnnnnn',
   M  => 'nynnnnn',
   Tu => 'nnynnnn',
   W  => 'nnnynnn',
   Th => 'nnnnynn',
   F  => 'nnnnnyn',
   Sa => 'nnnnnny',
);	 

for (qw(Su M Tu W Th F Sa)) {
   my $days = TransDays($_);
   print "$days\n";
}

sub TransDays
{
    # This subroutine translates the days format for the nhSchedule -list -full output
    # into the <ynnnnnn> format required for thr nhReport command
    my $new_sched = shift;
    return($TransDays{$new_sched});
}
 
interesting example netman4u ... ideal for utilising binary logic...

I've implemented via a mapping hash so the following code, although not perfect, will be scaleable:

Code:
#!/usr/lib/perl

use strict; 

# binary mapping for Sunday - Friday
# the end result will be translated 
# y =1 and n = 0
my $mapping = {
	Su => 0b1000000,
	M  => 0b0100000,
	Tu => 0b0010000,
	W  => 0b0001000,
	Th => 0b0000100,
	F  => 0b0000010,
	Sa => 0b0000001
};

my $schedule ="M,Th,F,Sa";
my @schedule = split /,/, $schedule;

# default to no days selected
# bitwise OR vs $schedule
my $bin_var = 0b0000000;
foreach my $day (@schedule) {
    $bin_var = $bin_var | $mapping->{$day}; # bitwise OR
}

# binary representation of result
my $trans_day = sprintf ("%.7b",$bin_var);

# translate to 0,1 to n,y format
$trans_day =~ tr/01/ny/; 
print $trans_day;

hope this helps !
 
Kevin,
I'm not sure what you're trying to do there but your solution looks to only translate one day.
I know I started my week on the wrong day but if you feed my code something like "M,Th,Su" you'll get all the flags processed in one hit which I think was the plan.
Did I miss the point you were trying to make?





Trojan.
 
parkers,

I agree with your way of thinking (as my previous solution demonstrates) but it just seems a little pointless to process in binary and then translate when you could do it all in one hit.



Trojan.
 
TWB,

Nice to see you back [bigsmile]

I was under the impression that the sub routine is feed only one day of the week at a time. So either I missed the point or you did, netman4u will lets us know which one of us is confused, probably me. [upsidedown]
 
ahh, I see I was wrong:

M,Tu needs to translate to nyynnnn (Monday and Tuesday)

I'll go back to my padded cell now [3eyes]
 
nice solution Trojan !

This is an interestng contrast actually ... string manipulation vs mathematical
 
my redemption post:

Code:
my @days_of_week = qw(n n n n n n n); 
my %TransDays = (
   Su => 0,
   M  => 1,
   Tu => 2,
   W  => 3,
   Th => 4,
   F  => 5,
   Sa => 6,
);	 

my $days = TransDays('M,F,Su');
print "$days\n";

sub TransDays
{
    # This subroutine translates the days format for the nhSchedule -list -full output
    # into the <ynnnnnn> format required for thr nhReport command
   for (split(/,/,shift)) {
      $days_of_week[$TransDays{$_}]='y';
   }
   return(join('',@days_of_week));
}
 
Folks,

I had my solution back in post #2 but I'm glad you had some fun.

More to come.

Nick

If at first you don't succeed, don't try skydiving.
 
your method could be reduced considerably if you wanted to stick with it:

Code:
    sub TransDays
    {
        # This subroutine translates the days format for the nhSchedule -list -full output
        # into the <ynnnnnn> format required for thr nhReport command
        local $_ = shift;
        my ($Sunday,$Monday,$Tuesday,$Wednesday,$Thursday,$Friday,$Saturday) = qw(n n n n n n n); 
        $Sunday    = 'y' if (/Su/);
        $Monday    = 'y' if (/M/);
        $Tuesday   = 'y' if (/Tu/);
        $Wednesday = 'y' if (/W/);
        $Thursday  = 'y' if (/Th/);
        $Friday    = 'y' if (/F/);
        $Saturday  = 'y' if (/Sa/);
        return("$Sunday$Monday$Tuesday$Wednesday$Thursday$Friday$Saturday");
    }
 
That is better Kevin. Thanks.

If at first you don't succeed, don't try skydiving.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top