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

Dynamic foreach loop 3

Status
Not open for further replies.

ejaggers

Programmer
Feb 26, 2005
148
0
0
US
I have a foreach that key sorts a hash. The keys can be either all numeric or alpha-numeric.
Is there a way to not repeat the foreach, and dynamically sort? i.e.

my @sortTpye = ( ‘sort’, ’sort{$a<=>$b}’ );
foreach ( &{“$sortType[$i]”} %hash ) {

}
 
prex1, Oooooh I see what you mean now about the difference between the two. Thanks for pointing that out.
I beleive I need ($a<=>$b)||($a cmp $b) because the key will either be a num or an alpha but not both (i.e. not 1a).
Do you think this one will work?
It sorted: A,B,2,11 (correct).
But how would I sort: 2,11,A,B?
 
What you are doing will work only at certain conditions:
-your numbers can't be of the style '001'
-zero can't be included or it will sort as the first in the list
-negative numbers not allowed
-alphanumerics can't really be so, they need to be strictly alphabetic
-you can't directly get get the numbers before the letters, as you are asking.
Now you'll tell us that you meet all of the conditions, but your code stays on the dark side, it's not a good one IMO.
Anyway if you have a single mixed up hash, not 2 separated ones as you stated somewhere, you'll need somehow to decide whether a key is numeric or string
I can't see a simple solution for having numbers precede the strings: see perldata #scalar_values in the perl documentation for a treatment of the non easy issue of distinguishing (in general) strings and numbers.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
prex1, the actual app creates menues:
Menu1:
1 - hashval1
...
9 - hashval9

OR

Menu2:
cmd1 - hashval1
cmd2 - hashval2
etc.

The key count will probably never be over 22 (page size). That's why I think it should work. But if you think the code is on the dark side, what would you do different? I'm open for suggestions, but I'm off tomorrow, and will check back on Monday.
 
As I stated above, if you have two separate hashes, one numeric, the other one alphabetic, then it's easy. You should clearly state:
1)What kind of data (a full example) you need to process before the process under 2 is started
2)How exactly you need to transform or process those data
3)The type and sorting of data you need after the process under 2 is executed

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
prex1, you were right about my code, "being on the dark side". I tried it with a different routine and it sucked for air. It ddn't sort right like I thought it would. I hope I've fixed it now:

Code:
     foreach ( sort keys %menu ) {
        if ( /^\d+$/ ) {
             $keyTypeNum = 'numbersort';
             last;
        }       
        my $length  = length($_);
        $keyLength  = ( $length > $keyLength) ? $length : $keyLength;
        push(@keys,$_);
     }
     
     if ( $keyTypeNum ) {
           @keys = (); 
           foreach ( sort{$a<=>$b} keys %menu ) {      
               my $length  = length($_);
               $keyLength  = ( $length > $keyLength) ? $length : $keyLength;
               push(@keys,$_);
           }
     
     }
 
I understand from your code that your hash is either fully numeric or fully alphabetic (as in your original post, but you wandered somewhere else after that). A shorter way of doing the same
Code:
$_=each%menu;
if(/\D/){
  @keys=sort keys%menu;
}else{
  @keys=sort{$a<=>$b}keys%menu;
}
$keyLength=0;map{$keyLength=length if(length)>$keyLength}@keys;
This code, as yours, will raise warnings if there are keys not fully numeric in a numeric hash, so that you'll know that in case everything is not as expected.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
prex1, yeah you're right about me wandering a bit. Actually
I had a different problem that I didn't find until after my last post. Thanks for the above. I've seen you use map before, so I'm playing around and trying to understand how it works. Looks like it works like a foreach, but only for one statement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top