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 4 of 6 Topic 10985 of 11520
Post > Topic >>

Re: Properly displaying items from hash

by krahnj@[EMAIL PROTECTED] (John W. Krahn) Apr 24, 2008 at 05:12 AM

icarus wrote:
> I have two files: log_ca.txt and log_aa.txt
> contents of log_ca.txt:
> 
> 333333333->ca_filename3
> 444444444->ca_filename4
> 111111111->ca_filename1
> 222222222->ca_filename2
> 
> contents of log_aa.txt:
> 
> 111111111->aa_filename1
> 333333333->aa_filename3
> 222222222->aa_filename2
> 444444444->aa_filename4
> 
> The program extracts the values after the -> delimiter of both files
> Makes an association between the values on both of the files.
> 
> Meaning, this is desired output:
> 
> CA FILENAME => AA_FILENAME
> ---------------------------
> ca_filename1 => aa_filename1
> ca_filename2 => aa_filename2
> ca_filename3 => aa_filename3
> ca_filename4 => aa_filename4
> 
> 
> Outputs I'm getting
> (see "double pop" in code below for details)
> 
> CA FILENAME => AA_FILENAME
> ---------------------------
>  =>
> ca_filename3 => aa_filename3
> ca_filename4 => aa_filename4
> 
> and
> 
> (after adding the "double pop" below prints all records but still get
> the => delimiter)
> CA FILENAME => AA_FILENAME
> ---------------------------
>  =>
> ca_filename1 => aa_filename1
> ca_filename2 => aa_filename2
> ca_filename3 => aa_filename3
> ca_filename4 => aa_filename4
> 
> 
> Questions:
> How do I get the desired output without resorting to the 'double pop'?
> How do I get rid of the extra "=>"?
> 
> Thanks in advance.
> 
> 
> #!/usr/bin/perl
> use warnings;
> use strict;
> 
> my $ca_path = "log_ca.txt";
> my $aa_path = "log_aa.txt";
> 
> 
> my %final_re****t;
> my @[EMAIL PROTECTED]
> my @[EMAIL PROTECTED]
> 
> 
> open (CAFILE, $ca_path) or die $!;
> my @[EMAIL PROTECTED]
 = <CAFILE>;
> 
> open(AAFILE, $aa_path) or die $!;
> my @[EMAIL PROTECTED]
 = <AAFILE>;
> 
> 
> #sort arrays
> my @[EMAIL PROTECTED]
 = sort @[EMAIL PROTECTED]
> my @[EMAIL PROTECTED]
 = sort @[EMAIL PROTECTED]
> 
> my $total_items = @[EMAIL PROTECTED]
> 
> foreach(@[EMAIL PROTECTED]
){
> 	s/[\r\t\n]+//; #Remove carriage returns and new lines
> 	my @[EMAIL PROTECTED]
 = split (/\d+->/, $_);

Assuming that $_ contains '111111111->ca_filename1' then @[EMAIL PROTECTED]
 now 
contains ( '', 'ca_filename1' ).


> 	push @[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
> }

At the end of the loop @[EMAIL PROTECTED]
 will contain:

( '', 'ca_filename1', '', 'ca_filename2', '', 'ca_filename3', '', 
'ca_filename4' )


You need to store only the string after /\d+->/:

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


> foreach(@[EMAIL PROTECTED]
){
> 	s/[\r\t\n]+//; #Remove carriage returns and new lines
> 	my @[EMAIL PROTECTED]
 = split (/\d+->/, $_);
> 	push @[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
> }

As above, at the end of the loop @[EMAIL PROTECTED]
 will contain:

( '', 'aa_filename1', '', 'aa_filename2', '', 'aa_filename3', '', 
'aa_filename4' )


> ###problems start here
> #why do I need to put pop@[EMAIL PROTECTED]
 twice?

Because @[EMAIL PROTECTED]
 has twice as many elements as @[EMAIL PROTECTED]
 
(where $total_items was derived from.)

> #otherwise it won't display all items.
> #I don't put that this is the outcome.
> #CA FILENAME => AA_FILENAME
> # =>
> #ca_filename3 => aa_filename3
> #ca_filename4 => aa_filename4
> 
> 
> for (1..$total_items){
> 	$final_re****t{ pop @[EMAIL PROTECTED]
 } = pop @[EMAIL PROTECTED]
> 	$final_re****t{ pop @[EMAIL PROTECTED]
 } = pop @[EMAIL PROTECTED]
> }

And so you end up with a hash that has a key '' and a corresponding 
value ''.

You could accomplish the same thing more simply with a hash slice (no 
loop required):

@[EMAIL PROTECTED]
 @[EMAIL PROTECTED]
 } = @[EMAIL PROTECTED]
> #why do I get the '=>' symbol there?
> print "CA FILENAME => AA_FILENAME\n";
> foreach (sort { $a cmp $b } keys(%final_re****t) ){
>     print "$_ => $final_re****t{$_}\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
 




 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 Thu Jul 24 13:16:36 CDT 2008.