Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^7: warning: use of uninitialized value

by robby_dobby (Hermit)
on Jun 26, 2015 at 05:05 UTC ( [id://1132060]=note: print w/replies, xml ) Need Help??


in reply to Re^6: warning: use of uninitialized value
in thread warning: use of uninitialized value

Hello mrityunjaynath,

Athanasius gave you the right advice about picking the correct values off split. What you need to realize is, arrays are indexed from zero: if you have an array @myarray with 3 elements, it will have elements at: $myarray[0], $myarray[1] and $myarray[2]. Also, why are you splitting on :: when you have specified the config file to be delimited by spaces?

$ cat pm_1132055.pl #!perl while(my $line = <DATA>) { chomp $line; my @words = split /\s+/, $line; print join(", ", @words), "\n"; print qq{\$words[1] --> $words[1] and \$words[3] --> $words[3]}, " +\n"; } __DATA__ ChipId 1925 SubVersid 0001 $ perl pm_1132055.pl ChipId, 1925, SubVersid, 0001 $words[1] --> 1925 and $words[3] --> 0001
Now, consider what you have done:
$ cat pm_1132055.pl #!perl while(my $line = <DATA>) { chomp $line; my @words = split /::/, $line; print join(", ", @words), "\n"; print qq{\$words[1] --> $words[1] and \$words[3] --> $words[3]}, " +\n"; } __DATA__ ChipId 1925 SubVersid 0001 $ perl pm_1132055.pl ChipId 1925 SubVersid 0001 $words[1] --> and $words[3] -->

Replies are listed 'Best First'.
Re^8: warning: use of uninitialized value
by mrityunjaynath (Acolyte) on Jun 26, 2015 at 05:26 UTC

    my fault...i have less knowledge of html so not able to write properly..what i want to say is my config file reads like this,
    chipId::1925
    subversid::0001
    now this is creating the above stated problem

      Well, in that case - you can do something like this:
      #!perl my %data_ids; while(my $line = <DATA>) { chomp $line; my ($key, $value) = split /::/, $line; $data_ids{$key} = $value; print $key, " => ", $value, "\n"; } for my $key (keys %data_ids) { print $key, " ==> ", $data_ids{$key}, "\n"; } # or just print $data_ids{'chipId'}, "\n"; print $data_ids{'subversid'}, "\n"; __DATA__ chipId::1925 subversid::0001

      From your original code sample, I surmise that you need these IDs for generating an m4 macro. Using a hash might be useful for you since you can just look that up when generating the m4 file later on. Bear in mind all points raise in this thread, use close, closedir properly.

      This got longer than expected. Next time around, you could save us all a lot of back and forth by including a small data sample as I showed above with __DATA__. Also, work through perlintro or an introductory perl book(learn.perl.org lists quite a few of them and some freely available online) and ask more questions here when you get stuck.

      Good luck!

        hello robby_dobby... thanks for the help..i am at the initial level so your suggestion are helping a lot
        i have tried your suggestion and changed the code like this

        my $cChipId; my $cSubVersId; my %data_ids; my $readline; if(open (my $CFILE,"config.txt" ) || die "couldnt open file:", $! ) { while ($readline = <$CFILE>) { chomp $readline; my ($key , $value) = (split /::/, $readline); $data_ids($key) = $value; $readline = <$CFILE>; } foreach my $key (keys %data_ids) { $cChipid = $data_ids('chipid'); $cSubVersId = $data_ids('subversid'); } } close($CFILE);

        but its showing some error like
        global symbol "$data_ids" requires explicit package name at newperlfile2.pl line85. syntax error at newperlfile2.pl line 85, near "$data_ids("
        ....please suggest...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1132060]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-18 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found