INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Looking for a way to enhance the implementation
2

Looking for a way to enhance the implementation

Looking for a way to enhance the implementation

(OP)
I have a piece of codes constructing an email sender:

CODE

sub getSender {
  my $host = hostname;a # use Sys::Hostname;
  my $domain = 'xyz.com';
  my $char = '.';
  my $from;
  if($host =~ /$domain/) {
    # hostname() may return a string like foo.bar.xyz.com and in this case
    # we want to return foo@bar.xyz.com 
    my $ind = index($host, $char);
    my $p1 = substr($host, 0, $ind);
    my $p2 = substr($host, $ind+1);
    $from = $p1.'@'.$p2;
  }
  else {
    # hostname() simply returns 'foo'
    $from = $host.'@'.$domain;
  }
  return $from;
} 

The blue section does not look pretty to me. Is there a way to enhance it using one-liner?

Many thanks.

RE: Looking for a way to enhance the implementation

Hi

Like this ?

CODE --> Perl

while (chomp($data = <DATA>)) {
  printf "%-15s | %-20s | %s\n", $data, &getSender(), &getSender2();
}

sub hostname { $data }

sub getSender {
  my $host = hostname; # use Sys::Hostname;
  my $domain = 'xyz.com';
  my $char = '.';
  my $from;
  if($host =~ /$domain/) {
    # hostname() may return a string like foo.bar.xyz.com and in this case
    # we want to return foo@bar.xyz.com 
    my $ind = index($host, $char);
    my $p1 = substr($host, 0, $ind);
    my $p2 = substr($host, $ind+1);
    $from = $p1.'@'.$p2;
  }
  else {
    # hostname() simply returns 'foo'
    $from = $host.'@'.$domain;
  }
  return $from;
}

sub getSender2 {
  my $host = hostname; # use Sys::Hostname;
  my $domain = 'xyz.com';
  my $char = '.';
  my $from = $host =~ /\Q$char\E(.*\Q$domain\E)$/ ? "$`\@$1" : "$host\@$domain";
  return $from;
}

__DATA__
foo.bar.xyz.com
foo
xyzwcom
abcxyzwcom
xyzwcomabc 

CODE --> output

foo.bar.xyz.com | foo@bar.xyz.com      | foo@bar.xyz.com
foo             | foo@xyz.com          | foo@xyz.com
xyzwcom         | xyzwco@xyzwcom       | xyzwcom@xyz.com
abcxyzwcom      | abcxyzwco@abcxyzwcom | abcxyzwcom@xyz.com
xyzwcomabc      | xyzwcomab@xyzwcomabc | xyzwcomabc@xyz.com 

Feherke.
feherke.ga

RE: Looking for a way to enhance the implementation

(OP)
I love it. Thank you, Feherke!

RE: Looking for a way to enhance the implementation

(OP)
Hi, Feherke,

I have a follow-up question.

Some people like one-liner because it's sophisticated. Some people hate it because it's hard to read and sometimes less efficient (e.g. one-liner to check a prime number). In the case above, getSender() vs. getSender2(), what's your opinion? Which way do you like better and why?

Many thanks!

@feherke

RE: Looking for a way to enhance the implementation

Hi

Well, I started coding on XT with 640 Kb RAM and 80x25 text mode display. These 2 are already enough to prefer compact code.

If that was not enough slow, I had the opportunity to code abit on Enterprise too, where the slowness of IS-Basic ( although is still the strongest Basic I ever met ) really put you on thinking.

For example have you played coding something interactive where you moved a cursor with (72)/(80)/(75)/(77) keys ( in parenthesis their scan codes ) ?

Well, usually such thing is implemented with 2 scalar variables for the 2 coordinats and a switch statement to decide which coordinate needs to be changed :

CODE --> Perl

use 5.10.1;
no warnings 'experimental';

$x = 10;
$y = 10;
foreach $key (72, 80, 75, 77, 80, 77, 80, 77, 80) {
    given ($key) {
        when(72) { $y-- }
        when(80) { $y++ }
        when(75) { $x-- }
        when(77) { $x++ }
        default { print 'something else' }
    }
}

print "$x : $y\n"; # outputs 12 : 13 

Or you can use 1 array with 2 elements for the 2 coordinates and use 2 expressions : 1 to choose the array item to change and 1 to calculate the direction :

CODE --> Perl

@c = (10, 10);
foreach $key (72, 80, 75, 77, 80, 77, 80, 77, 80) {
    given ($key) {
        when([72, 80, 75, 77]) { $c[abs($key - 76) > 1] += ($key > 76) * 2 - 1 }
        default { print 'something else' }
    }
}

print "$c[0] : $c[1]\n"; # outputs 12 : 13 

The 1st is readable, the 2nd is faster. At least in IS-Basic. Of course, this is probably not true / not obvious for modern machines and languages.

The conclusion ? I prefer compact code because my experiences taught me to appreciate them. You better do how your boss / employer prefers.

Feherke.
feherke.ga

RE: Looking for a way to enhance the implementation

(OP)
Thank you, Feherke, for your explanation. It helps a lot!

RE: Looking for a way to enhance the implementation

(OP)
Hi Feherke,

I want to follow up on this thread.

Let's say hostname() returns a host w/o domain name like this: foo.bar. I have to admit this is a rare hostname and I have never seen it before. But it's possible, right?

My question is how to still return the $from as 'foo@bar.xyz.com'? I noticed that the current implementation would return foo.bar@xyz.com. And this should be ok. However, for the sake of learning one-liner implementation, could you please show me how to return 'foo@bar.xyz.com'?

I just want to learn how to write a sophisticated one-liner perl.

Many thanks!

RE: Looking for a way to enhance the implementation

Hi

Sorry, I do not get it. What is the rule now, in human terms ?

Feherke.
feherke.ga

RE: Looking for a way to enhance the implementation

(OP)
Hi Feherke,

Sorry for that I did not make myself clear. Let me restate my question:

The hostname() may return one of the following four values:
1) foo
2) foo.xyz.com
3) foo.bar.xyz.com
4) foo.bar

In either case, I want to return $from as foo@xyz.com or foo@bar.xyz.com in one-liner perl.
Your original solution handles the first three cases well, but not the forth case (I know it's a rare one and I did not mention this case in my original thread).

Could you please show me an enhanced one-liner implementation to include the forth case?

Thanks.

RE: Looking for a way to enhance the implementation

Hi

This still not explains the rule : what to check and where to cut.

Anyway, let us try this :

CODE --> Perl ( fragment )

my $from = $host =~ /\Q$char\E(.*?)(\Q$domain\E)?$/ ? "$`\@$1" . ($2 || ".$domain") : "$host\@$domain"; 

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close