undef in Hash of Arrays

Discussion in 'Perl Misc' started by Deepu, Jun 25, 2006.

  1. Deepu

    Deepu Guest

    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
     
    Deepu, Jun 25, 2006
    #1
    1. Advertising

  2. Deepu

    Anno Siegel Guest

    Deepu <> 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
     
    Anno Siegel, Jun 25, 2006
    #2
    1. Advertising

  3. Deepu

    Guest

    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
     
    , Jun 25, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,140
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  3. Adam Akhtar
    Replies:
    5
    Views:
    653
    Adam Akhtar
    Mar 25, 2008
  4. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    673
    Älphä Blüë
    Jul 18, 2009
  5. Tim McDaniel

    undef($foo) versus $foo = undef()?

    Tim McDaniel, Aug 19, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    146
    Peter J. Holzer
    Aug 19, 2009
Loading...

Share This Page