Gossamer Forum
Home : General : Perl Programming :

push new element to array

Quote Reply
push new element to array
HI. Senior

@myNames = ('Moe', 'Shemp', 'skie','blah');
$newdefined = $new;


my question is
if $new is not included in array @myNames, then push (@myNames, $newdefined);
if $new is included in array @myNames then print" $newdefined was registered \n";

i have been considered for ........ next...
while eventually i found $newdefined has been
pushed mutiple times while i only want to adds elements $newdefined to the array one time only
('Moe', 'Shemp', 'skie','blah' , '$new');


any one could light me a bit?

Thanks in advance
Quote Reply
Re: [courierb] push new element to array In reply to
Hi,

Below should work

@myNames = ('Moe', 'Shemp', 'skie','blah');
$newdefined = $new;
my %myNames = map{$_ => 1} @myNames ;
if(exists $myNames{"$newdefined"}){
print "$newdefined was registered";
}else{
push(@myNames,$newdefined );
}

Hope that helps!

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] push new element to array In reply to
Another example...

Code:
if (grep $newdefined eq $_, @myNames) {
print "$newdefined already registered!";
}
else {
push (@myNames, $newdefined);
}
Quote Reply
Re: [Wychwood] push new element to array In reply to
thanks for everyone .i will go for a try


i am try to get it to work in another approach. while still got the problem


for($i=0; $i<=10;$i++)
{

$i ==5 or next; #### $i ==100 or $i == 5
my $swithnew = "old";
printf " $i next\n";
printf "$i swithnew= $swithnew xxx\n";
}

if ($swithnew=null ) { printf "$i it is an old value. we need to splice the array with new value\n"; }
else { printf " $i it is an new value. we have to puch the new value to the array \n"; }


----------------------
when i set $i ==5
the script print out 11 it is an old value. we need to splice the array with new value
this is the result i am expected

while i set $i ==100
the script pint out "11 it is an new value. we have to puch the new value to the array " which is out of my expectatioin result. what i expected is " it is an new value. we have to puch the new value to the array "


what is wrong with the code?

Thank you in advance
Quote Reply
Re: [Wychwood] push new element to array In reply to
thanks for Wychwood. the code is great for me .
Quote Reply
Re: [tandat] push new element to array In reply to
Thanks for tandat

The code is working. thank you very much
Quote Reply
Re: [tandat] push new element to array In reply to
how to lookup if specified key in a hash is exist ?
#@myNames = ('Moe', 'Shemp', 'skie','blah');
$newduserid=newuseridentered
%myNames = (
userid1 => Moe, 20, male
userid2 => Shemp, 30,male
userid3, => skie, 40,,male
userid4 => blah ,50,male);


if newduserid= userid1 then
print" $userid1 is most probabley has been registed before. \n";

else print
print" you are encouraged to registe again. \n";


I wonder if there are any company code much like Wychwood and tandat shows us?
what i really want is prevent from using loop.

Last edited by:

courierb: Oct 1, 2007, 1:44 AM
Quote Reply
Re: [courierb] push new element to array In reply to
Hi,

If you want to lookup a key of a hash, this should work

Code:

#@myNames = ('Moe', 'Shemp', 'skie','blah');
$newduserid=newuseridentered
%myNames = (
userid1 => Moe, 20, male
userid2 => Shemp, 30,male
userid3, => skie, 40,,male
userid4 => blah ,50,male);


if (exists $myNames{$newduserid}){
print" $userid1 is most probabley has been registed before. \n";

}else{ print
print" you are encouraged to registe again. \n";
}


I hope that is what you want.

Cheers,

Cheers,

Dat

Programming and creating plugins and templates
Blog

Last edited by:

tandat: Oct 1, 2007, 5:37 AM
Quote Reply
Re: [tandat] push new element to array In reply to
HI man. it works like my expectation.

Thank you very much. Smile
Quote Reply
Re: [tandat] push new element to array In reply to
It seems something wrong with the code " my $output = $session->param('USERS');"
if this is the correct way to load session data?


Code:

my $newduserid = 100;
my $output = $session->param('USERS');
if (exists $output {$newduserid}){ print" $userid1 is most probabley has been registed before. \n"; }else{ print print" you are encouraged to registe again. \n"; }

the session data look like
USERS => [{name => 'name1' ,userid=> 1},{name => 'name2' ,userid=> 2}]
Quote Reply
Re: [courierb] push new element to array In reply to
Hi,

It seems that $USER is an array ref of hash ref. So, you need to use an index to access it. Some thing like $user1 = $USER->[0]; then access to its name and userid vai $user1->{name} and $user1->{userid};

Hope that help!

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] push new element to array In reply to
it is mean i have to using looping to checking?

thus any existed userid entered. will print one time of print" $userid1 is most probabley has been registed before. \n";
while it will print mutiple times of print" you are encouraged to registe again. \n";


actually i want print one times of print" you are encouraged to registe again. \n";
much like tandat show me the code by using

if (exists $myNames{$newduserid}){
print" $userid1 is most probabley has been registed before. \n";

}else{ print
print" you are encouraged to registe again. \n";
}[/code]
Quote Reply
Re: [courierb] push new element to array In reply to
it seems to worked now by this way.



for ($count=1; $count<11; $count++)
{

$count==100 or next; ##count = 5 (existed value ) or count = 100 (new value)

$COUNTNEXT=$count; #this line has been skiped if count>10

print"COUNT1 = $count !--\n";
}

print"FINAL COUNT = $count COUNTNEXT=($COUNTNEXT) !--\n";

if ($COUNTNEXT ==""){ print"new value shoule be pushed now-\n";}
else {print"this is old old value do nothing.\n";}