How do I check for an empty scalar in Perl? - perl

How do I check for an empty scalar in perl? If I have no $list, I do not want to send an email.
Can I check for empty message in the send_email routine or do this outside?
I have a query that uses Win32::OLE.
my $servSet = $wmiObj->ExecQuery("SELECT * FROM Win32_Service WHERE DisplayName LIKE 'ServiceNameHere%'", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
I'm looping through it here and building a list $list
foreach my $serv (in $servSet) {
next if $serv->{started};
my $sname = $serv->{name};
my $sstate = $serv->{started};
my $ssmode = $serv->{startmode};
$list .= "Service: $sname - $sstate - $ssmode\n";
}
I use the $list to send as body of the email:
sub send_email {
...
..
$smtp->datasend($list);
..
.
}

In Perl, undef, "" (and also 0 and "0") evaluate to "false". So you can just do a boolean test:
send_email() if $list;

I don't like to fool around with what's actually in the variable. If I want to see if anything, anything at all, is in a scalar, I check its length:
send_mail() if length $scalar;

Have you tried this?
if (!($list eq ""))
send_email(...);
or
if ($list ne "")
send_email(...);

Related

Blank result for String concatenation in foreach

There might not be the best way but what is wrong with this code. Why do i get blank result from $str in the foreach loop, whereas if it try to concatenate individual cells i get the right result.
$csv = import-csv -Path "C:\Users\abc\csv4script\TEST.csv" -Header 'IPs'
$str = ''
$x = 1
foreach ($cell in $csv.){
if ($x -le 4000){
$str = $str + ", " + $cell.IPs
if ($x -eq 4000){
$x = 1}
$str = ''
}
$x = $x + 1
}
$str
# $str = $str + $csv[1].IPs + ", " + $csv[2].IPs
$str
It doesn't have a value because you misplaced a '}' character.
$x = 1}
Move it to after $str = ''
$x = 1
$str = ''}
Also as Thomas pointed out, remove the period after $csv variable in your foreach statement, it should be like: foreach($cell in $csv)
Also if you want to increment a value,
$x++ is quicker and easier to read than $x = $x + 1
Lastly, as you have it, if your list has more than 4000 IPs the $str variable will empty out.

powershell double for loop

Need advice on loop
$Variable contains 11111 22222
foreach ($variable in $value) {
for ([byte]$c = [char]'b'; $c -le [char]'c'; $c++) {
$variable."([char]$c)" } }
I am looking output as 11111b and then 22222c but currently, I am getting 11111b , 11111c and then 22222b and then 22222c.
Kindly advice
I am assuming you mean that $value, not $variable, contains 11111 and 22222, specifically in an array.
Since you want $c to maintain its value between iterations of the foreach loop you need to initialize $c outside of the foreach loop. Therefore, you really don't need (or, rather, should not use) two loops at all.
$value = 11111, 22222;
[Byte] $c = [Char] 'b';
foreach ($variable in $value)
{
"$variable$([Char] $c++)"
}
This gives the output you are seeking:
11111b
22222c

If condition not matching inside a foreach loop in perl

I'm trying to match a string with if statement inside a foreach loop but its not matching although i get the same string with printed before if statement inside foreach loop. Please help.
use Net::Telnet;
$ip='xx.xxx.xx.xx';
$ip_port='10002';
$port = new Net::Telnet->new( Host=>$ip,Port=>$ip_port,Dump_log=> "dump.log");
my #folder= $port->cmd("ls");
sleep(2);
$folders=#folder;
print "Number of folders are:$folders\n";
foreach my $folder(#folder)
{
print "Folder before if is:$folder\n";
if(($folder eq "acc") || ($folder eq "bda"))
{
# some code here.
}
}
Your strings probably contain white space. You can use something like chomp to remove it, or alternatively use regexs.
Try:
if ($folder =~ /^(acc|bda)/) {
# some code here
}

Perl IF statement not matching variables in REGEX

my $pointer = 0;
foreach (#new1)
{
my $test = $_;
foreach (#chk)
{
my $check = $_;
chomp $check;
delete($new1[$pointer]) if ($test =~ /^$check/i);
}
$pointer++;
}
The if statement never matches the fact that many entries in the #new1 array do contain $check at the start of the array element (88 at least).
I am not sure it is the nested loop that is causing the problem because if i try this it also fails to match:
foreach (#chk)
{
#final = (grep /^$_/, #new1);
}
#final is empty but I know at least 88 entires for $_ are in #new1.
I wrote this code on a machine running Windows ActivePerl 5.14.2 and the top code works. I then (using a copy of #new1) compare the two and remove any duplicates (also works on 5.14.2). I did try to negate the if match but that seemed to wipe out the #new1 array (so that I didn't need to do a hash compare).
When I try to run this code on a Linux RedHat box with Perl 5.8.0 it seems to struggle with the variable matching in the REGEX. If I hard code the REGEX with an example I know is in #new1 the match works and in the first code the entry is deleted (in the second one value is inserted in #final).
The #chk array is a listing file on the web server and the #new1 array is created by opening two log files on the web server and then pushing one into the other.
I had even gone to the trouble of printing out $test and $check in each loop iteration and manually checking to see if any of the the values did match and some of them do.
It has had me baffled for days now and I have had to throw the towel in and ask for help, any ideas?
As tested by user1568538, the solution was to replace
chomp $check;
with
$check =~ s/\r\n//g;
to remove Windows-style line endings from the variable.
Since chomp removes the contents of the input record separator $/ from the end of its argument, you could also change its value:
my $pointer = 0;
foreach (#new1)
{
my $test = $_;
foreach (#chk)
{
local $/="\r\n";
my $check = $_;
chomp $check;
delete($new1[$pointer]) if ($test =~ /^$_/i);
}
$pointer++;
}
However, since $/ also affects other operations (such as reading from a file handle), perhaps it is safest to avoid changing $/ unless you are sure if it is safe. Here I limit the change to the foreach loop where the chomp occurs.
No knowing what your input data looks like, using \Q might help:
if ($test =~ /^\Q$check/i);
See quotemeta.
It is not clear what you are trying to do. However, you may be trying to only get those elements for which there is no match or vice versa. Adapt the code below for your needs
#!/usr/bin/perl
use strict; use warnings;
my #item = qw(...); # your #new?
my #check = qw(...); # your #chk?
my #match;
my #nomatch;
ITEM:
foreach my $item (#item) {
CHECK:
foreach my $check (#check) {
# uncomment this if $check should not be interpreted as a pattern,
# but as literal characters:
# $item = '\Q' . $item;
if ($item =~ /^$check/) {
push #match, $item;
next ITEM; # there was a match, so this $item is burnt
# we don't need to test against other $checks.
}
}
# there was no match, so lets store it:
push #nomatch, $item.
}
print "matched $_\n" for #matched;
print "didn't match $_" for #nomatch;
Your code is somewhat difficult to read. Let me tell you what this
foreach (#chk) {
#final = (grep /^$_/, #new1);
}
does: It is roughly equivalent to
my #final = ();
foreach my $check (#chk) {
#final = grep /^$check/, #new1;
}
which is equivalent to
my #final = ();
foreach my $check (#chk) {
# #final = grep /^$check/, #new1;
#final = ();
foreach (#new) {
if (/^$check/) {
push #final, $_;
last;
}
}
}
So your #final array gets reset, possibly emptied.

Using a Perl hash

It's the first time I've used a hash in Perl, and I'm stuck in a weird problem. What I'm trying to do is after I backup files in a directory, I use a Perl program to check if all files appearin the log file. So I had the following code:
our (%missing_files) = (); # global definition on the top of the program
... do something ...
sub CheckTarResult {
my (#dir_list) = (); # dir list
my (#file_list) = (); # will be filled with all file names in one dir
my ($j) = "";
my ($k) = ""; # loop variable
my ($errors) = 0; # number of missing files
... do something ...
foreach $j (#dir_list) {
#file_list = `ls $j`;
foreach $k (#file_list) {
$result = `cat $logfile | grep $k`;
if ($result eq "") {
$errors++;
$missing_files{$j} = ${k};
}
}
#file_list = ();
}
... do something ...
my($dir) = "";
my($file) = "";
while ( ($dir, $file) = each(%missing_files) ) {
print $dir . " : " . $file;
}
I made an empty log file to do the test, the expecting result should give me all files missing, but somehow "missing_files" only stores the last missing file in each dir. The logic seems to be straightforward, so what am I missing here?
Edit:
I used the advice from #Borodin, and it worked. But in order to print the content of an array reference, we need to loop through elements in the array. The code after the change looks like the following:
... everything before is the same ...
push #{$missing_files{$j}}, ${k}; # put elements in dictionary
# in the print statement
while( ($dir, $file) = each(%missing_files) ) {
for $i ( 0 .. $#$file ) { # $#$file represents the array size by reference
print $dir . " : " . ${$file}[i];
}
}
Perl hash values can contain only a single scalar. If you want to store a list of things then you must make that scalar an array reference. To do that, change the line
$missing_files{$j} = ${k};
to
push #{$missing_files{$j}}, ${k};