Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: Properly di...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 6 Topic 10985 of 11532
Post > Topic >>

Re: Properly displaying items from hash

by Uri Guttman <uri@[EMAIL PROTECTED] > Apr 25, 2008 at 06:12 PM

>>>>> "I" == Icarus  <rsarpi@[EMAIL PROTECTED]
> writes:

  I> Thanks everybody...here is the final code I crafted from my original
  I> and your suggestions.

  I> #!/usr/bin/perl
  I> use warnings;
  I> use strict;

good.

  I> my $ca_path = "log_ca.txt";
  I> my $aa_path = "log_aa.txt";


  I> my %final_re****t;
  I> my @[EMAIL PROTECTED]
  I> my @[EMAIL PROTECTED]
't declare things before they are used/needed.

  I> open (CAFILE, $ca_path) or die $!;
  I> my @[EMAIL PROTECTED]
 = <CAFILE>;

  I> open(AAFILE, $aa_path) or die $!;
  I> my @[EMAIL PROTECTED]
 = <AAFILE>;

use File::Slurp ;

	my @[EMAIL PROTECTED]
 = read_file( $aa_path ) ;

simpler and faster. also look below as how it can be used

  I> #sort arrays
  I> my @[EMAIL PROTECTED]
 = sort @[EMAIL PROTECTED]
  I> my @[EMAIL PROTECTED]
 = sort @[EMAIL PROTECTED]
 are you sorting now before you extract the filenames? i haven't
followed this thread closely but this seems wrong to me. but i will go
with it.

you can sort on the output of readfile:

	my @[EMAIL PROTECTED]
 = sort read_file( $aa_path ) ;

  I> my $total_items = @[EMAIL PROTECTED]
 don't need that temp variable. see below

  I> foreach(@[EMAIL PROTECTED]
){
  I>      s/\s+\z//;  # Remove all trailing whitespace
  I>      push @[EMAIL PROTECTED]
 /\d+->(.+)/;
  I> }

you can extract the filename and remove the whitespace in one go:

my @[EMAIL PROTECTED]
 = map /\d+->(.+)$/, @[EMAIL PROTECTED]
 ;

that assumes a simple newline at the end. do you really have more white
space there?

and another merger:


my @[EMAIL PROTECTED]
 = map /\d+->(.+)$/, sort read_file( $aa_path ) ;

hmm, one line for seven so far!
of course duplicate this for the other file. i left that to you as an
exercise.

  I> for (1..$total_items){

for( 1 .. @[EMAIL PROTECTED]
 ){

you can just use the array size here.

  I> 	$final_re****t{ pop @[EMAIL PROTECTED]
 } = pop @[EMAIL PROTECTED]
  I> }

why the loop and pop? try a simple slice:

	@[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
 } = @[EMAIL PROTECTED]
 assignment of all the keys/values.


  I> print "CA FILENAME => AA_FILENAME\n";
  I> print '-' x 27, "\n";
  I> foreach (sort { $a cmp $b } keys(%final_re****t) ){

no need for the cmp as sort defaults to a string compare. 

  I>     print "$_ => $final_re****t{$_}\n";
  I> }

just a different way to do that:

	print map "$_ => $final_re****t{$_}\n", sort keys %final_re****t ;

uri

-- 
Uri Guttman  ------  uri@[EMAIL PROTECTED]
  --------  http://www.sysarch.com
--
-----  Perl Code Review , Architecture, Development, Training, Sup****t
------
--------- Free Perl Training --- http://perlhunter.com/college.html
---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com
---------
 




 6 Posts in Topic:
Properly displaying items from hash
rsarpi@[EMAIL PROTECTED]   2008-04-23 18:35:29 
Re: Properly displaying items from hash
peng.kyo@[EMAIL PROTECTED  2008-04-24 19:09:13 
Re: Properly displaying items from hash
noreply@[EMAIL PROTECTED]  2008-04-24 13:51:16 
Re: Properly displaying items from hash
krahnj@[EMAIL PROTECTED]   2008-04-24 05:12:57 
Re: Properly displaying items from hash
Uri Guttman <uri@[EMAI  2008-04-25 18:12:23 
Re: Properly displaying items from hash
rsarpi@[EMAIL PROTECTED]   2008-04-24 12:17:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 6:23:28 CDT 2008.