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!

map { } and "next"

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi,

I've got a bit of a problem :/

Basically, I'm looping a hashref with;

Code:
map {
 ...
} keys %$var;

However, if you do;

Code:
map {
 .. do something
  if (something) { 
    next; 
  }
} keys %$var;

Basically, I just need to know what the map{} equivilant of "next" is?

TIA

Andy
 
There isn't one! Normally you'd use grep to filter out the elements you don't want, and then use map to alter them.

You seem to be using map in a void context there: can you post the actual code and perhaps we can suggest an alternative?
 
Hi,

Thats what I thought :/

Well, here is some actual example code;

Code:
    map {
        my ($username,$site) = split /--/, $_;
        if ($seen->{$_} > 1) {

        print "\n";
        print "===" x 20;
        print "\n";

       if (ref $user ne "HASH") {
         next;
       }
       if (!$user->{Username}) {
         next;
       }

#  .. more stuff here
   } keys %$users;

...and I've also tried;

Code:
    LOOPSUBS: map {
        my ($username,$site) = split /--/, $_;
        if ($seen->{$_} > 1) {

        print "\n";
        print "===" x 20;
        print "\n";

       if (ref $user ne "HASH") {
         next LOOPSUBS;
       }
       if (!$user->{Username}) {
         next LOOPSUBS;
       }

#  .. more stuff here
   } keys %$users;

TIA

Andy
 
Using map in a void context doesn't really make logical sense, since the purpose of map is to transform list elements in some way. Here, you can just use a for() loop, like so:

Code:
for ( keys %$users ) {
        my ($username,$site) = split /--/, $_;
        if ($seen->{$_} > 1) {

        print "\n";
        print "===" x 20;
        print "\n";

       if (ref $user ne "HASH") {
         next;
       }
       if (!$user->{Username}) {
         next;
       }

#  .. more stuff here
   } 
[code]
 
Hi,

Yeah, its a bit of a funny old script <G> It spans between 5 sites, and due to this, the "key" in the hashref is the value of the site, user and ID.

I've changed it to a for() and its working great now.. thanks =)

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top