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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help in polishing my code 2

Status
Not open for further replies.

lcs01

Programmer
Aug 2, 2006
182
US
Requirements:
=============
There is an array containing unknown number of elements. Among these elements, one could be 'aaa' and the other 'bbb'. The rest elements are less significant.

If this array does contain 'aaa' and/or 'bbb', a new new array '@r' will be created whose last element is 'aaa' and the 2nd to the last is 'bbb'.

The following is my implementation:

Code:
use strict;

my @o = ('asd','wer','111','a1a2','gergh','aaa','111awre','cxvb','rewtrh','bbb','fsdgfsg','rtut');
print "\$#o = $#o\n";

my (@tmp, @n);
foreach my $e (reverse @o) {
  if ($e eq 'bbb') {
    push @tmp, $e;
  }
  else {
    unshift @tmp, $e;
  }
}
foreach my $e (reverse @tmp) {
  if ($e eq 'aaa') {
    push @n, $e;
  }
  else {
    unshift @n, $e;
  }
}
my $o = join ',', @o;
my $r = join ',', @n;
warn "#$o#\n#$r#\n";

And here is the output:
Code:
$#o = 11
#asd,wer,111,a1a2,gergh,aaa,111awre,cxvb,rewtrh,bbb,fsdgfsg,rtut#
#asd,wer,111,a1a2,gergh,111awre,cxvb,rewtrh,fsdgfsg,rtut,bbb,aaa#

As you can see, the output is correct. But the implementation looks dumb. Could someone polish my code, please?

Many thanks!!
 
Do you always want to leave the original elements in the same order they were in minus the aaa bbb??



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
How about a custom sort function ?

Code:
print join ',', sort by_myfunction ('asd','wer','111','a1a2','gergh','aaa','111awre','cxvb','rewtrh','bbb','fsdgfsg','rtut');

sub by_myfunction($a,$b) {
  return $b eq 'aaa' ? -1 : $a eq 'aaa' ? 1 : $b eq 'bbb' ? -1 : $a eq 'bbb' ? 1 : 0 ;
}
 
Your code is fine. However, you could self-document it by giving the variables meaningful names.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@orig[/blue] = [red]qw([/red][purple]asd wer 111 a1a2 gergh aaa 111awre cxvb rewtrh bbb fsdgfsg rtut[/purple][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\$[/b][/purple]#orig = [blue]$#orig[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [red]([/red][blue]@filtered[/blue], [blue]@postfix[/blue][red])[/red][red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$element[/blue] [red]([/red][blue]@orig[/blue][red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$element[/blue] eq [red]'[/red][purple]bbb[/purple][red]'[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/unshift.html][black][b]unshift[/b][/black][/url] [blue]@postfix[/blue], [blue]$element[/blue][red];[/red]
		
	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red][blue]$element[/blue] eq [red]'[/red][purple]aaa[/purple][red]'[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@postfix[/blue], [blue]$element[/blue][red];[/red]
		
	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[black][b]push[/b][/black] [blue]@filtered[/blue], [blue]$element[/blue][red];[/red]
	[red]}[/red]
[red]}[/red]

[black][b]push[/b][/black] [blue]@filtered[/blue], [blue]@postfix[/blue][red];[/red]

[black][b]my[/b][/black] [blue]$orig[/blue] = [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple],[/purple][red]'[/red], [blue]@orig[/blue][red];[/red]
[black][b]my[/b][/black] [blue]$filtered[/blue] = [black][b]join[/b][/black] [red]'[/red][purple],[/purple][red]'[/red], [blue]@filtered[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [red]"[/red][purple]#[blue]$orig[/blue]#[purple][b]\n[/b][/purple]#[blue]$filtered[/blue]#[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
is this school/class/course work?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you all for your help and reply! And special thanks go to brigmar and Miller!!

To travs69:

No, the order is less important as long as 'bbb' & 'aaa' are in order.

To Kevin,

No, it is not a homework. I wish I was still in school. :)

Actually, I am working on an industry specific file management system which allows clients searching documents by various criteria, including full text search which is the most time consuming. So, if a full text search is combined with some other searches then we want to make sure that other searches are always conducted first so that a full text search can be done in a limited range provided that the connector is 'AND'. That's where my question in the original post was derived from.

BTW, please pardon me to ask this – what is wrong to ask a homework related question here? Suppose someone got a question from a textbook, could s/he ask for help here?

Again, thank you all for your great help!
 
Oh, I forgot one more thing...

Thank you, Miller, for your advice. Actually, I always use meaningful names for all variables/subroutines at work. I am the only one in my group to strictly stick to this rule. :)
 
what is wrong to ask a homework related question here?"

Cheating on homework, getting people here to give answers instead of working it out, is what's not allowed.

Mike

When working on any project the value of other people is exactly that - they are other people, with views that don't necessarily match yours. This mismatch, between their views and the view you've been contentedly assuming is right, is where that value lies.
 
what is wrong to ask a homework related question here?

No student posting is allowed here at all. Those are the rules setup by the founders and adminstrators of Tek-Tips. The site is intended for IT professionals. There are plenty of other sites that students can post questions on but there are few where IT professionals only are allowed to post, in fact this is the only site I know of dedicated to the IT professional.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top