CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Comparing a scalar constant with a list( Array ).

 



vishy27
Novice

Jun 1, 2010, 9:16 PM

Post #1 of 7 (1318 views)
Comparing a scalar constant with a list( Array ). Can't Post

Hi,

I am comparing a constant, in this case the current working directory with a list of directories.

EX :

@Right_dir = </system/hardware/FW/*/*> ;

Here the above array holds the list of directories under FW folder.

$currDir = `pwd`;
chomp $currDir;

The above scalar holds the current directory.

I want the program to continue with the rest of the code if any one of the directories match when comparing @Right_dir and $currDir.


Code
 
sub perl_lib() ; # perl_lib is a sub-routine here

foreach (@Right_dir){

perl_lib if ( $currDir ne $_ ) ;

}


I used the above code to compare each element of the array with the $currDir but it keeps going to perl_lib sub-routine . Frown

I used all kinds of logic that came to my dinosaur brain but to no avail.

Please help me with this.

Thanks!
vishy27


MetalGear
Novice


Jun 1, 2010, 11:06 PM

Post #2 of 7 (1313 views)
Re: [vishy27] Comparing a scalar constant with a list( Array ). [In reply to] Can't Post

Try putting some print statements,


Code
  

sub perl_lib() ;

foreach my $elem (@Right_dir)
{
print "$elem = $currDir";

perl_lib if ( $currDir ne $elem ) ;
}



vishy27
Novice

Jun 1, 2010, 11:41 PM

Post #3 of 7 (1312 views)
Re: [MetalGear] Comparing a scalar constant with a list( Array ). [In reply to] Can't Post

Hey MetalGear,

Thanks for Replying so fast !!

anyways i tried to print the elements as u said and if the very 1st element of the array does'nt match it goes into perl_lib sub-routine.

currently the folder structure is like this( only an Example ) :

@Right_dir holds this

/system/hardware/FW/change1/remote

/system/hardware/FW/change2/local



so if i run the script from the second folder it prints

/system/hardware/FW/change2/local = /system/hardware/FW/change1/remote

and goes into the perl_lib sub-routine.

Is there any way to skip the sub-routine even if one element matches ?

Hope u get this.

Thanks again


BillKSmith
Veteran

Jun 2, 2010, 4:30 AM

Post #4 of 7 (1299 views)
Re: [vishy27] Comparing a scalar constant with a list( Array ). [In reply to] Can't Post


Code
 

if ( !grep $curDir eq $_), @Right_dir {

perl_lib;

}



For a slightly better solution, see the CPAN module List::MoreUtils


Code
   

use List::MoreUtils qw(none);

if ( none( $cur_dir eq $_, @Right_dir ) ) {

perl_lib;

}



(Corrected arguments of grep)
Good Luck,
Bill

(This post was edited by BillKSmith on Jun 2, 2010, 6:22 AM)


vishy27
Novice

Jun 2, 2010, 5:01 AM

Post #5 of 7 (1297 views)
Re: [BillKSmith] Comparing a scalar constant with a list( Array ). [In reply to] Can't Post

Hey BillKSmith,

Thanks for that reply,

But i get the warning,

Use of uninitialized value in string eq at <file_path> line 28

any idea how to skip this?


shawnhcorey
Enthusiast


Jun 2, 2010, 5:33 AM

Post #6 of 7 (1295 views)
Re: [vishy27] Comparing a scalar constant with a list( Array ). [In reply to] Can't Post


In Reply To
I want the program to continue with the rest of the code if any one of the directories match when comparing @Right_dir and $currDir.


First, always put these two lines in your code:

Code
use strict; 
use warnings;


Second, you cannot compare a scalar to an array. The best you can hope for is that the scalar is an element of the array. Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

use Cwd;

my @Right_dir = grep { -d } glob( '/system/hardware/FW/*/*' );
my $currDir = cwd();

exit unless grep { $_ eq $CurrDir } @Right_dir;


See:
perldoc -f glob
perldoc -f grep
perldoc Cwd

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


vishy27
Novice

Jun 2, 2010, 9:30 PM

Post #7 of 7 (1280 views)
Re: [shawnhcorey] Comparing a scalar constant with a list( Array ). [In reply to] Can't Post

 
Hi shawnhcorey,

Thanks for the info shawn. My script is working beautifully nowWink.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives