Trailing 1 being inserted?

I was messing around with the beginnings of a simple script and noticed early one that the output I expected to see whas there but it has a trailing "1" inserted on the end. Why is this 1 being added and how do I get rid of it?

[code]
#!/usr/bin/perl

use strict;
my($check, @results);

chomp($check = `id -g`);

print $check . "\n";

@results = print $check;

print @results . "\n";
[/code]

if you are a normal user it is suppose to report back:

500
500

but it does not. This is what I get back:

500
5001

Has an extra 1 on the end.
LVL 5
ZiaTioNAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
Not sure what you are trying to do with the assignmnet

@results = print $check;

If you want to check the success of the id call, use

my $check = `id -g` or die "Can not run id command $!\n";

If you want to add the value of $check to the @results array, use

push @results,$check;
ZiaTioNAuthor Commented:
What I am trying to dow ith that call is load the results od the id -g command into the array.

So when I enter "id -g" I want whatever the system returns as the result of that query to be added to that array. I am not sure pushing it is the way to go. As a matter of fact I am pretty sure it is not what I am looking for.

if the system returns my group id as 500 when I "print @results" I want it to print 500. However it does not it prints 5001.
ahoffmannCommented:
think you meant:
  @results = split(/\s+/,$check);
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

ZiaTioNAuthor Commented:
Still no go. Your suggestion and the push suggestion only load the "1" into the array and not the results of the id command.

ZiaTioNAuthor Commented:
#!/usr/bin/perl

use strict;
my($check, @results);

chomp($check = `id -g`);

print $check . "\n";

@results = split(/\s+/,$check);

print @results . "\n";


This is the updated code with the split method.

Here is the output when the script is ran.

[ziation@ToughGuy ziation]$ perl -c access_checker
access_checker syntax OK
[ziation@ToughGuy ziation]$ ./access_checker
500
1
[ziation@ToughGuy ziation]$
ahoffmannCommented:
hmm, would be nice if you tell us what you expect, and what is wrong
ahoffmannCommented:
> .. the results of the id command
aha, you want the gid, why not using:
 $g=(split(/\s+/,$())[0]; print $g__
ZiaTioNAuthor Commented:
I thought I already did that in my original post to start this thread. Also I thought it would be very clear from looking at the source code but I guess not.

I want the value of the @results array to be what the system returns as a response to the query "id -g".

The response if the screipt is ran by a user with normal user permissions would be 500 but I am getting "1" as the value of the array @results.

Can you not see that in the sample script execution I supplied in my previous post?
ZiaTioNAuthor Commented:
>> .. the results of the id command
>aha, you want the gid, why not using:
 >$g=(split(/\s+/,$())[0]; print $g__


I did not use that because to tell you the truth I have no idea what that even does. LOL...

Care to explain it? If it works I would love to use it.
ozoCommented:
print @results . "\n";
is the same as
print scalar @results ."\n";
you probably wanted
print @results, "\n";
# note , instead of .
FishMongerCommented:
@results = print $check;

That line is doing 2 things (3 depending on how you break it down);
   1) prints the value of $check
   2) assigns the value of $check to first element of the @results array
   3) it also adds/appends the failure/sucsess status of the print statement (which is 0 or 1) to the array element.

Since you already have the output of your id command in the $check scalar, I'm not sure why you're assigning it a 1 element array.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
inq123Commented:
Hi ZiaTioN,

It seems a simple case (wonder why so many posts?), the 1 is the result of your statement:

print @results . "\n";

Here @results has only one data member, so it's a 1 in scalar context.  Your @results = print $check basically first print 500, and then your print @results . "\n" prints "1\n", thus the line became 5001\n which is what you saw.

Did I miss something?

Cheers!
inq123Commented:
FishMonger,

Seems you beat me by seconds in this post. :=)  It's funny to see this simple case puzzled two big experts.  No offense, though.
ZiaTioNAuthor Commented:
Well you might have not missed something but what I missed was the fact that just assigning the array the print statement would actually print the value which I did not know.

@results = print $check;

This is what I was using just to load the results into the array and did not know that it would print it also.
FishMongerCommented:
You probably already figured this out, but if you want to put the value of $check into an array, you'd use either one of these statements.

@results = $check;

push @results, $check;

The first one will will accomplish what you're currently doing.  The second one would be used (in a loop) to build up the array with multiple elements.


inq123,
Yes, this time I was able to post seconds prior to you, but with my current work schedule, I'm not able to spend as much time in here as I would like.  So, you'll probably be beating me more often. :-)
inq123Commented:
Fishmonger, I found I tend to disagree with you, and this time's no exception. :-)  I don't understand why OP wanted @results = print $check;  But if OP wants the return of print stored in @results, but at the same time do not want the string to be printed actually to STDOUT, I think the closest thing that can be done is to open a filehandle for /dev/null, and print to that filehandle.  But then again, there's no real way of faithfully simulating the return status of print to STDOUT unless you actually print to STDOUT.

BTW, FishMonger, if I want to beat you on posts, I can probably do so these couple days since I'm on vacation at home. That'll be tough for you to beat. :-)  But I also intend to relax a bit so I'm not really very active as I found out time flew so fast when you're on vacation. :-(
ZiaTioNAuthor Commented:
Thanks alot for the help guys. I had figured out (with your guys help) already how to push the return value of $check into @results. Now I have a problem when comparing it to the value of another array I have in the script.

Here is the code:

------------------code-----------------------------

#!/usr/bin/perl

use strict;
my($check, @results, @gid, $timestamp, @timestamp);

chomp($check = `id -g`);

if (-e "gid_log") {
   open(file, "<gid_log") || die "Cannot open gid_log";
   @gid = <file>;
   close(file);
   @results = $check;
}else{
   print `touch gid_log`;
   open(file, ">gid_log") || die "Cannot open gid_log";
   @results = print file $check . "\n";
   close(file);
   open(file, "<gid_log") || die "Cannot open gid_log";
   @gid = <file>;
   close(file);
}

if (@results == @gid) {
   print "They are the same!\n";
}else{
   chomp($timestamp = `date`);
   if (-e "notify.txt") {
      open(notify, ">>notify.txt") || die "Cannot open notify.txt";
      @timestamp = print notify "Your access was removed at " . $timestamp . "\n";
      close(notify);
   }else{
      print `touch notify.txt`;
      open(notify, ">>notify.txt") || die "Cannot open notify.txt";
      @timestamp = print notify "Your access was removed at " . $timestamp . "\n";
      close(notify);
   }
}

----------------------end code---------------------------------------------

When I compare @gid to @results to see if they equal each other it gets satisfied and prints the statement "They are the same!" and exits. Now this is suppose to happen when they are the same but if I open up gid_log after it is created by the script anc change the value of the integer inside to say like 5 and run the script @gid should have a value of 5 and@results should have a value of 0 (if ran as root) so the if statement should not be satisfied and should jump to else but it does not. It still prints out the statement saying they are the same when they are not.

Any ideas why?
FishMongerCommented:
if (@results == @gid)

That tests if they have the same nunber of elements.  It doesn't look at their values.  Use scalars insread of the arrays.

  $gid = <file>;
  $results = $check;
if ($results == $gid)

or

if ($check == $gid)
TintinCommented:
Let's rewrite your code to be a more Perl literate.  I've also removed all the redudant code (arrays, if tests etc):

#!/usr/bin/perl
use strict;

my $check = `id -g` or die "Can not run id command $!\n";
my $gid;

if (-e "gid_log") {
   open FILE, "gid_log" or  die "Can not open gid_log $!\n";
   $gid = <FILE>;
   close FILE;
} else {
    open FILE, ">gid_log" or die "Can not open gid_log $!\n";
    print FILE $check;
    close FILE;
}

chomp ($gid,$check);

if ($gid == $check) {
   print "They are the same!\n";
} else {
   open NOTIFY, ">>notify.txt" or  die "Can not open notify.txt $!\n";
   print NOTIFY "Your access was removed at " . localtime() . "\n";
   close NOTIFY;
 }
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.