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:
Print Hash Keys

 



Stefanik
User

Jan 3, 2013, 12:50 AM

Post #1 of 10 (1397 views)
Print Hash Keys Can't Post

Hi all, I've should print out the hash keys to a file, separated with newline:


Code
my $KeyHa = $value1; 
my $ValHA = 1;
$Ha_ha{$KeyHa} = $ValHA;

open (PARSEOUTFILE, ">", $FileParsed) or die "No file!";
@Ha_KeyArr = keys %Ha_ha;
print PARSEOUTFILE "@Ha_KeyArr\n";



In output file I have the key printed with a space " " at the begin:

" key1 key2"

While I'd like to have:

"key1

key2"

Can you help me?


Laurent_R
Veteran / Moderator

Jan 3, 2013, 4:43 AM

Post #2 of 10 (1394 views)
Re: [Stefanik] Print Hash Keys [In reply to] Can't Post

Hi,

one way to do it:


Code
# ... 
$Ha_KeyScalar = join "\n", keys %Ha_ha;
print PARSEOUTFILE "$Ha_KeyScalar";



FishMonger
Veteran / Moderator

Jan 3, 2013, 6:47 AM

Post #3 of 10 (1389 views)
Re: [Stefanik] Print Hash Keys [In reply to] Can't Post

Here are 3 methods. There are more ways to do it, but my preference is the second one, however, I'd use a lexical filehandle instead of the bareword.

Code
print PARSEOUTFILE "$_\n" for keys %Ha_ha;



Code
# if using perl 5.10.0 or above 
say PARSEOUTFILE $_ for keys %Ha_ha;



Code
my @keys = keys %Ha_ha; 
$" = "\n";
print "@keys";



Stefanik
User

Jan 3, 2013, 6:59 AM

Post #4 of 10 (1387 views)
Re: [FishMonger] Print Hash Keys [In reply to] Can't Post

Thanks to all of you Smile

Is it possible to have an hash key with character empty (" " or "") ?

In that case to erase it, should I use a loop:


Code
  if (($value1 != " ") && ($value1 != ""))  
{
my $KeyHa = $value1;
my $ValHA = 1;
$Ha_ha{$KeyHa} = $ValHA;
}



Or is it possible to execute the check while I assign the variable to the Hash-key?


(This post was edited by Stefanik on Jan 3, 2013, 7:08 AM)


FishMonger
Veteran / Moderator

Jan 3, 2013, 7:47 AM

Post #5 of 10 (1382 views)
Re: [Stefanik] Print Hash Keys [In reply to] Can't Post

It would be best to validate your data prior to adding it to the hash, but if that wasn't done, then you can use the delete function to remove the unwanted key/value pair from the hash.


Stefanik
User

Jan 3, 2013, 1:29 PM

Post #6 of 10 (1372 views)
Re: [FishMonger] Print Hash Keys [In reply to] Can't Post

Ok I find out the "empty" lines are the ones does not match the condition.
So I try, as suggest to delete at the begin:

Code
open (PARSEFILE, "<", $FileToParse) or die "No file!"; 
while ($FirstPar = <PARSEFILE>){
@Parse_Arr = $FirstPar unless ( $FirstPar =~ m/Mystring1|Mystring2/ );
print @Parse_Arr;
}
close (PARSEFILE);


In this case the lines in the file that match Mystring1 or Mystring2 doesn't print in array, instead are duplicated the previous line. Unimpressed

Why? Unsure


7stud
Enthusiast

Jan 3, 2013, 1:50 PM

Post #7 of 10 (1370 views)
Re: [Stefanik] Print Hash Keys [In reply to] Can't Post

...because you only assign a new value to @Parse_Arr when there is no match, yet you print every time through the loop. So if there is a match, you don't assign a new value to @Parse_Arr, and so @ParseArr has the old value, i.e. from the previous line, which you then print.

You want something like this:


Code
 

next if $FirstPar =~ /Mystring1|Mystring2/ ;

print @Parse_Arr;


That way the print statement is skipped if there is a match.


By the way, your variable names are terrible. See how much clearer this code is:


Code
use strict;  
use warnings;
use 5.012;

my $fname = 'data.txt';

open my $INFILE, "<", $fname
or die "Couldn't open $fname: $!";

while (my $line = <$INFILE>){
next if $line =~ /string1|string2/;
print $line;
}

close $INFILE;

Generally, you don't capitalize your variable names. Some people capitalize variable names that contain references to file handles, so that they look similar to bareword filehandles(which you shouldn't be using).


(This post was edited by 7stud on Jan 3, 2013, 2:11 PM)


Stefanik
User

Jan 3, 2013, 2:39 PM

Post #8 of 10 (1352 views)
Re: [7stud] Print Hash Keys [In reply to] Can't Post

Thanks 7stud for the information and suggest.

Just one think it's not clear for me: to assign the value in "while" loop to Array, should I increment Array index, or is it performed automatically?

Thanks


FishMonger
Veteran / Moderator

Jan 3, 2013, 2:48 PM

Post #9 of 10 (1350 views)
Re: [Stefanik] Print Hash Keys [In reply to] Can't Post

You use the push function to add item onto an array.

http://perldoc.perl.org/functions/push.html


Stefanik
User

Jan 4, 2013, 12:16 AM

Post #10 of 10 (1342 views)
Re: [FishMonger] Print Hash Keys [In reply to] Can't Post

Thanks again.

 
 


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

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