develooper Front page | perl.beginners | Postings from May 2008

Re: assign default value to variables I assign from split

Thread Previous | Thread Next
From:
John W. Krahn
Date:
May 14, 2008 15:10
Subject:
Re: assign default value to variables I assign from split
Richard Lee wrote:
> Dr.Ruud wrote:
>>
>> But better stop guessing and let Richard answer.
> 
> yes, variables are particular names and later I wanted to refer back by 
> variable names.
> 
> However, for now I have done this so far so I just added as array 
> instead of breaking out by var names..
> -- code is incomplete (meaning I haven't finished yet) --
> 
> use warnings;
> use strict;
> use Data::Dumper;
> 
> my $directory = q#/home/server1/arch#;
> my @array = qx/ls -tr $directory | tail -1/;

You are only getting one file name (tail -1) so why are you assigning it 
to an array?  It would be more efficient to do that directly in perl. 
Also you haven't removed the newline.


> my @hh;
> for ( @array ) {
>        my $file = $_;

Usually written as:

for my $file ( @array ) {

Since @array only has one file name in it why do you need the loop?


>        open FILE, "ls -tr | zcat -d $directory/$file |", or die qq/you 

My version of zcat does not have a -d switch, what does it do on your 
system?  It appears that "ls -tr | " in front of zcat is superfluous? 
What do you think it will do there?


> truly suck $!\n/;
>        my $count;
>        my %hh;
>        while (<FILE>) {
>             chomp;
>             next if /^$/;
>             next if /^#/;
>  
>             my @array   = map $_||'default', (split 

Why does this array have the same name as the other array in file scope?


> /\|/)[3,4,6,7,12,40,41,42,43,46,56,64];
>             next if $array[0] eq 'default';
>             $array[10] =~ s/\,/\-/g;

More efficient as:

              $array[10] =~ tr/,/-/;


>             push @hh, \@array;
>        }
>        close FILE;

When you close a piped open you should verify that the pipe closed 
correctly:

          close FILE
              or warn $! ? "Error closing ls pipe: $!"
                         : "Exit status $? from ls";


> }
> 
> system("clear");
> print 
> "===================================================================================================\n\n"; 
> 
> 
> my %missing_address;
> my %duration_report;
> 
> for (@hh) {
>    my $yahoo = $_;

Usually written as:

for my $yahoo ( @hh ) {


>    $missing_address{$$yahoo[1]}++ if $$yahoo[8] =~ /^default$/ and 
> $$yahoo[0] != m/espn|google|msn/;
>    $$yahoo[4] +=

$$yahoo[] is usually written as $yahoo->[].

$$yahoo[8] =~ /^default$/ is usually written as $yahoo->[8] eq 'default'.

$$yahoo[0] != m/espn|google|msn/ is incorrect, you probably meant 
$yahoo->[0] !~ m/espn|google|msn/.


> }
> 
> if (%missing_cics) {
>   for (keys %missing_address) {
>       print "MISSING ADD: $_ : $missing_address{$_} \n";
>   }
> }
> 
> for (keys %duration_report) {
>     print "total duration $_  : $duration_report{$_}\n";
> }


John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

Thread Previous | Thread Next


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About