Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

strict

by toadi (Chaplain)
on Nov 22, 2000 at 15:35 UTC ( [id://42904]=perlquestion: print w/replies, xml ) Need Help??

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

In my attempt to use strict I get errors with unitialized variables .Here's the sample data:
testuser Authentication-Type = Unix-PW User-Service = Framed-user, Ascend-Idle-Limit = 600, Framed-Protocol = PPP, Framed-Address = 222.222.222.222, Framed-Netmask = 255.255.255.255, Ascend-Route-IP = Route-IP-Yes, Ascend-Metric = 3 test2user Password = "Testpassword" Framed-Protocol = PPP, Ascend-Idle-Limit = 600
I just wanted to parse the file to get all the values.
#!/usr/bin/perl -w # # ##################################################################### use strict; open (FILE,"<users.merit") or die ("users_merit nie ge-opend"); my ($login,$password,$user_service,$framed_protocol,$framed_address,$f +ramed_netmask,$ascend_route_IP,$ascend_metric,$ascend_idle_limit); while(<FILE>){ next if /^#/; chomp; if($_ =~ /^\w/){ m/(.*)(Password = |Authentication-Type = )(.*)/; ($login,$password) = ($1,$3); $login =~ s/\s+$//; $password =~ s/\"//g; }elsif($_ =~ /^\s/){ if($_ =~ /(User-Service = )(.*)(,)/){ $user_service = $2; } if($_ =~ /(Framed-Protocol = )(.*)(,)/){ $framed_protocol = $2; } if($_ =~ /(Framed-Address = )(.*)(,)/){ $framed_address = $2; } if($_ =~ /(Framed-Netmask = )(.*)(,)/){ $framed_netmask = $2; } if($_ =~ /(Ascend-Route-IP = )(.*)(,)/){ $ascend_route_IP = $2; } if($_ =~ /(Ascend-Metric = )(.*)(,)/){ $ascend_metric = $2; } if($_ =~ /(Ascend-Idle-Limit = )(.*)(,)/){ $ascend_idle_limit = $2; } } print "$login:$password\n"; print "$user_service\n"; print "$framed_protocol\n"; print "$framed_address\n"; print "$framed_netmask\n"; print "$ascend_route_IP\n"; print "$ascend_metric\n"; print "$ascend_idle_limit\n\n"; } close FILE;
I get always undefined values because I try to print out variables that haven't a value. Erm this is what I think is the problem.

error:Use of uninitialized value in string

Replies are listed 'Best First'.
Re: strict
by autark (Friar) on Nov 22, 2000 at 16:20 UTC
    First, it is not use strict; which enables these warnings, but rather -w. Anyway, I agree with your theory that it must be the print statements trying to use variables without values.

    There are a couple of things you should be aware of in this code. First, you try to print out all the values each time you have read a line from the file. This will of course generate warnings about the use of uninitialized values.

    Second, you should probabely use a datastructure to hold your data. As it stands now, you will only get the last record of data, and it will not be correct. It will probabely contain data from earlier records.

    A reasonable datastructure to use in this case might possibly be to use a hash of hashes

    %data = (user1 => { "User-Service" => "Framed-User", "Framed-Protocoll" => "PPP", # etc... }, user2 => { ... } );
    Autark.
      yes that was the idea ... This maybe solves the problem.
      See I program bit by bit try to do one thing then add a next one if the first one worked.

      I will pump it in a hash, but then still the problem comes up. I THINK :)

      My opinions may have changed,
      but not the fact that I am right

        When you use a hash, you can assign values to keys,
        if there is no value, there is no key, then you can just
        print out all the keys

        And else you just check if the values are defined before
        printing/doing something with them

        GreetZ!,
          ChOas

        I didn't know my example was THAT bad
Re: strict
by ChOas (Curate) on Nov 22, 2000 at 16:14 UTC
    Ooooohkay, quick and dirty, how I would start: no error checking,
    and clumsy, but maybe an Idea:
    #!/usr/bin/perl -w use strict; my $UserID="Unknown"; my %UserData; while (<>) { next unless (/\w/io); #Skip empty lines if (/^(\S+)\s+(\S+)\s*=\s*(\S+)/io) # Match <NewUser> <Tag>=<Data> to + $1,$2,$3 { $UserID=$1; #Set $UserID to current user $UserData{$UserID}{$2}=$3; #Assign the data to the user in a + hash next; #Next line, this was a special ca +se }; s/\s+|\,//iog; #Remove spaces, and comma's (God +forgive me for that one) my ($Key,$Val)=split /=/; #Get the Key, and Data $UserData{$UserID}{$Key}=$Val; #Assign it to the current user }; #Just some printing of the data. foreach my $User (keys %UserData) { print "User: $User\n"; foreach my $Data (keys %{$UserData{$User}}) { print "\t\"$Data\":\t$UserData{$User}{$Data}\n"; }; };
    With your example data this gives me:
    choas@<Censored to protect myself from my employer>$ ./lal inpt User: test2user "Password": "Testpassword" "Framed-Protocol": PPP "Ascend-Idle-Limit": 600 User: testuser "User-Service": Framed-user "Authentication-Type": Unix-PW "Framed-Address": 222.222.222.222 "Framed-Protocol": PPP "Ascend-Metric": 3 "Ascend-Route-IP": Route-IP-Yes "Framed-Netmask": 255.255.255.255 "Ascend-Idle-Limit": 600

    Hope I gave you an Idea

    GrtZ!
      ChOas

    p.s. yeah, most of my quick 'n dirty progs are called 'lal' ;))
Re: strict
by rpc (Monk) on Nov 22, 2000 at 23:40 UTC
    Here's how I would do it:
    #!/usr/bin/perl -w use strict; use Data::Dumper; open FILE, "users.merit" or die "can't open users.merit."; my $fieldreg = qr/([^(?:\s=\s)]+)\s=\s([^,]+)/; my %users; my $user; while(<FILE>) { chomp; # next user? if(/(\S+)\s+$fieldreg/) { $user = $1; $users{$user}{$2} = $3; next; } if(/$fieldreg/) { $users{$user}{$1} = $2; } } print Dumper(\%users);
    $0.02 :)
Re: strict
by curtisb (Monk) on Nov 23, 2000 at 03:56 UTC
    Here is something simple. It is not much, pretty basic. yes, you can get this to work without strict, it is meant to be an example of how to use it.
    #!/usr/bin/perl -w use strict; my $count = 0; do { print "$count "; $count++; }while($count < 10)
    curtisb -- "Trying to help!"
      Erm, no offence. But I know how to use strict:)
      The problem was with udefined variables. I wanted to print out, but I've builded a nice hash to store the data in.

      But thanx for the effort.<BT>
      --
      My opinions may have changed,
      but not the fact that I am right

        Ok, i just thought that I would try to help with a simple example. I wasn't trying to say anything was wrong, with your example. Just trying to keep is simple. Hashes were a good way to go.
        curtisb

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-25 22:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found