[28014] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 9378 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jun 25 18:10:15 2006

Date: Sun, 25 Jun 2006 15:10:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 25 Jun 2006     Volume: 10 Number: 9378

Today's topics:
        undef in Hash of Arrays <pradeep.bg@gmail.com>
    Re: undef in Hash of Arrays (Anno Siegel)
    Re: undef in Hash of Arrays mgarrish@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 25 Jun 2006 11:07:29 -0700
From: "Deepu" <pradeep.bg@gmail.com>
Subject: undef in Hash of Arrays
Message-Id: <1151258849.034457.219090@i40g2000cwc.googlegroups.com>

Hi All,

I have a file which have transitions like:

STATE1
STATE3
STATEMAIN  ####
STATESUSPEND
STATE1
STATE2
STATERESUME
RESET
STATE6
STATE8
STATEMAIN  ####
STATE9
STATE10

This indicates a transition from STATE1 to STATE3, then STATE3 to
STATEMAIN and so on. There are also 10 files which will have different
combinations of these states.

Here i need to go through the file and check for STATEMAIN and when i
see STATEMAIN, then i need to put it in an array till i see RESET or
end of file. After that again i need to proceed further to check
whether there is any more STATEMAIN. If present again i need to put it
in a different array till RESET or end of file.

Code:

use strict;
use Data::Dumper;

my @files = qw/FILE1 FILE2/;

my %transitions;
my %hashsuspend

foreach my $filename (@files)
{
    my $fh;
    open $fh, $filename
        or do {
            warn "$filename missing";
            next;
        };

    my $run_through = 0;
    my $recording_enabled = 0;
    while (<$fh>)
    {
        if (/^RESET\b/)
        {
            $recording_enabled = 0;
        }
        elsif (/^STATEMAIN\b/)
        {
            $run_through++;
            $recording_enabled = 1;
            $suspendCount =0;
        }
        elsif ($recording_enabled)
        {
            push @{$transitions{$filename}[$run_through]}, $_;
            if ($_ =~ /^STATESUSPEND/)
            {
               $suspendCount++;
               $hashsuspend{$filename}[$run_through] = $suspendCount;
               next;
            }
        }
    }
    close $fh or die $!;

}

print Dumper \%transitions;
print Dumper \%hashsuspend;

The output i am getting with this is:

%transitions = (
                             FILE1 => [
                                              undef,
                                              [
                                                  STATESUSPEND
                                                  STATERESUME
                                                  STATE1
                                                  STATE2
                                              ],
     and so on.

What is the reason for undef at the start of the array in every
file.This undef appears even with hashsuspend. Please help me on this.

Thanks
-- Deep



------------------------------

Date: 25 Jun 2006 19:11:54 GMT
From: anno4000@radom.UUCP (Anno Siegel)
Subject: Re: undef in Hash of Arrays
Message-Id: <4g85fqF1m8ittU1@news.dfncis.de>

Deepu <pradeep.bg@gmail.com> wrote in comp.lang.perl.misc:
>Hi All,
>
>I have a file which have transitions like:
>
>STATE1
>STATE3
>STATEMAIN  ####

We've been through this, haven't we, under the subject "Hash of Arrays".
What happened to the useful advice you got in that thread?  John W. Krahn
offered a wonderful solution in essentialy 4 lines, among other things.

Anno


------------------------------

Date: 25 Jun 2006 12:21:04 -0700
From: mgarrish@gmail.com
Subject: Re: undef in Hash of Arrays
Message-Id: <1151263264.599783.54840@y41g2000cwy.googlegroups.com>


Deepu wrote:
>
>     my $run_through = 0;

>     while (<$fh>)
>     {

>         elsif (/^STATEMAIN\b/)
>         {
>             $run_through++;
>             $recording_enabled = 1;

>         }
>         elsif ($recording_enabled)
>         {
>             push @{$transitions{$filename}[$run_through]}, $_;


>
> The output i am getting with this is:
>
> %transitions = (
>                              FILE1 => [
>                                               undef,
>                                               [

>
> What is the reason for undef at the start of the array in every
> file.This undef appears even with hashsuspend. Please help me on this.
>

How about looking at your code above bearing in mind that arrays start
at 0 not 1.

Matt



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 9378
***************************************


home help back first fref pref prev next nref lref last post