On Thu, Aug 6, 2009 at 11:49 AM, Telemachus <telemac...@arpinum.org> wrote:
> On Thu Aug 06 2009 @ 11:19, jet speed wrote: > > @array1 = ( D_101 D_102 D_103 D_104); > > @array2 = (0 1 2 3); > > > > > > How can i convert both of these arrays into %hash, assigining the > > @array1 as keys and @array2 as values. > > use warnings; > use strict; > my @array1 = qw/D_101 D_102 D_103 D_104/; > my @array2 = (0, 1, 2, 3); > > my %hash = map { $array1[$_] => $array2[$_] } (0..$#array1); > > This would work, but in practice if @array2 held literally numbers from 0 > to the upper index of @array1, then it's unnecessary. You could do it > easily without a second array at all. If @array1 has numbers that aren't > simply the index, then you could do it as above. Otherwise, try this: > > my @array1 = qw/D_101 D_102 D_103 D_104/; > > my %hash = map { $array1[$_] => $_ } (0..$#array1); > > Also you should always use warnings and strict to catch errors. You should > be seeing errors about how you're trying to initialize the two arrays > (barewords in @array1 and missing operators - the commas - in @array2). > > > How can I recall only certain keys and their corresponding values of > hashes > > ex : if D_103 then print " D_103 value is 2" > > ex :if D_101 then print "D_101 value is 0" > > I'm not quite sure what you have in mind here, but in general printing a > key/value pair if you have the hash key in a variable (say $foo) is as easy > as this: > > print "The value of $foo is $hash{$foo}\n"; > > In this case, perhaps you narrow down the records you care about and then > put those into an array first: > > my @important_records = qw/D_102 D_104/; > > for my $item (@important_records) { > print "The value of $item is $hash{$item}\n"; > } > > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > ------------------------- Hi Guys, Fist of all thanks for taking time and answering my queries. With all of your help i managed to put togather the below script and get the desired output. #!/usr/bin/perl use warnings; system "tpconfig -dl >tpout"; my $filename = 'tpout'; open FILE, '<', $filename or die "Could not open $filename: $!"; my (@data, @drive); while ( <FILE> ) { push @data, $1 if / ^ # start of string \s+ # followed by whitespace Index # match literal string \s+ # followed by whitespace ( \d+ ) # match and capture any numerical digits /x; ####seperation############## push @drive, $1 if / ^ # start of string \s+ # followed by whitespace Drive\ Name # match literal string \s+ # followed by whitespace ( \w+ ) # match and capture any word characters /x; } ############################ my %hash = map { $drive[$_] => $data[$_] } (0..$#drive); print "BOTH KEY AND CORRESPONDING VALUE \n"; print "@{[ %hash ]}\n"; print " Selected KEY AND CORRESPONDING VALUE \n"; my @important_records = qw/B3494_901 B3494_310 B3494_300/; for my $item (@important_records) { print "The value of $item is $hash{$item}\n"; } "mod2" 45 lines, 1157 characters # ./mod2 BOTH KEY AND CORRESPONDING VALUE B3494_102 19 B3494_903 14 B3494_904 10 B3494_900 13 B3494_101 12 B3494_300 4 B3494_303 16 B3494_302 6 B3494_305 5 B3494_308 18 B3494_901 0 B3494_307 15 B3494_310 1 B3494_309 7 B3494_306 11 B3494_301 2 B3494_905 8 B3494_304 9 B3494_100 3 B3494_902 17 Selected KEY AND CORRESPONDING VALUE The value of B3494_901 is 0 The value of B3494_310 is 1 The value of B3494_300 is 4 Next step i would like to the seperate the 'value' into a format 0:1:4 from above and then pass it onto system command, as below. Kinldy let me know how can i achieve this. system "tpconfig -multiple_delete -drive 0:1:4 "; Appericate your help with this. Thanks