tilak_sai has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Hash accessing issue while returning hash from sub routinue
by TGI (Parson) on Oct 08, 2008 at 21:15 UTC

    Something like this untested code should work.

    sub Msg { my $line = shift; my ($pf,$fixFields) = split(/received:|Sent:/, $line); my %fixMsg = split(/[,=]/, $fixFields); @fixMsg{('_time_', '_mi_', '_fixsession_')} = (split(/\|/, $pf))[2 +,3,5]; return \%fixMsg; } open( my $file, '<', "abc.txt" ) or die "Unable to open data file - $!\n"; while(<$file>){ my $msg = pfUtils::Msg($_); my %skip_keys = map {$_ => undef} qw( _mi_ _fixsession_ _time_ ); foreach my $key ( sort keys %$msg ) { next if exists $skip_keys{$key}; my $value = $msg->{$key}; next unless defined $value and length $value; my $code = 'where did this come from?'; printf "\n\t%20s (%3d) = %s ", $code,$key,$value; } } close $file;

    I have some coding style suggestions for you.

     

     

    Use       less   white space. Most people don't use enough. You've gone too far in the opposite direction.

     

     

    Use more descriptive names. What's a pf? potato fragment? pig fertilizer?

    You have some variables declared with my, others are not. Are you using "strict" and "warnings"? You should be.

    Check the results of your calls. Especially to things like open that fail frequently.

    Spend some time reading some basic perl tutorials and perldoc. You have several very odd and incorrect usage in your post. For example, you are using bitwise or in your tests for hash keys to skip. Various resources can be found by searching PM. http://learn.perl.org has a good list, too.

    Read about how to post here. You will get a better response if you take the advice to heart. The article on effectively asking questions has good pointers that can be used to good effect in any online forum.

    To progress do these three things: keep reading, keep writing code, and keep asking questions.


    TGI says moo

Re: Hash accessing issue while returning hash from sub routinue
by Bloodnok (Vicar) on Oct 08, 2008 at 11:51 UTC
Re: Hash accessing issue while returning hash from sub routinue
by dHarry (Abbot) on Oct 08, 2008 at 12:26 UTC
    You could read the documentation...

    "The keys are returned in an apparently random order. The actual random order is subject to change in future versions of perl, but it is guaranteed to be the same order as either the values or each function produces (given that the hash has not been modified). Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons"

    I suggest you do a Super Search in the Monastery...

Re: Hash accessing issue while returning hash from sub routinue
by AZed (Monk) on Oct 08, 2008 at 19:43 UTC

    I'm having a little trouble working out exactly what your problem is, given that your code is very badly formatted, but I suspect you want one of two things:

    1. Use each to extract both key and value from the hash at the same time
    2. Use Tie::IxHash to have your hash always return its keys/values in the same order they were entered.
Re: Hash accessing issue while returning hash from sub routinue
by blazar (Canon) on Oct 14, 2008 at 22:19 UTC
    I am new to work hash tables in perl

    I personally believe that this is clear enough. I also believe you may be new to Perl in general. More precisely, when I read both "Hash accessing issue while returning hash from sub routinue" and "how to get the hash values and keys in same sequence while getting hash from sub routinue" even before proceeding, I feel the need to clarify: you can't do that. Specifically, subs can only return lists thus you can (typically) return a list of key-value pairs which will be assigned to a hash, or a hashref, which seems to be your case.

    Even with the code tags added, though, it's hard to understand what your actual problem may be: we have some code from you, and an excerpt from the file it's supposed to operate on, but no indication whatsoever of how does the former fail to work like you expected, nor -more simply- what you would expect. Your mention of "same sequence" suggests what AZed suspected too: anyway, I know I'm a bit late answering your question, but I see no intervention of yours in this thread beyond the root node, so chances are you may still want to clarify so as to help us to help you.

    --
    If you can't understand the incipit, then please check the IPB Campaign.