You should never use $1 without testing to see whether or not the regexp
found something, try:

while (<FILE>) { $users{$1} = "Offline" if (/^(\d{3})/) }

or the more verbose

while (<FILE>) {
        #if this line has a key
        if (/^(\d{3})/) {
                #then add it to the hash
                $users{$1} = "Offline"
        }
}

On 20 Jun 2001 15:07:17 -0500, Tom Yarrish wrote:
> Okay, I tried what you suggested, but I modified it a bit.  Here's what I
> put:
> 
> while(<FILE>) {
>         /^(\d{3})/;
>         $users{$1} = "Offline";
> }
> Then I do a foreach loop to print out the keys and the values.  That's
> fine, but for some reason I get a line like this:
> : Offline (note: WHen I print out the keys I separate them with a :)
> There's no number at the beginning.  I went through the file it's reading,
> and I can't figure out where it's getting that line from.  Is there an
> extra line being added to the hash?
> 
> Thanks,
> Tom
> 
> -- 
> #!/usr/bin/perl -w # 526-byte qrpff, Keith Winstein and Marc Horowitz
> <[EMAIL PROTECTED]> # MPEG 2 PS VOB file on stdin -> descrambled output
> on stdout # arguments: title key bytes in least to most-significant order
> $_='while(read+STDIN,$_,2048){$a=29;$c=142;if((@a=unx"C*",$_)[20]&48){$h=5;
> $_=unxb24,join"",@b=map{xB8,unxb8,chr($_^$a[--$h+84])}@ARGV;s/...$/1$&/;$d=
> unxV,xb25,$_;$b=73;$e=256|(ord$b[4])<<9|ord$b[3];$d=$d>>8^($f=($t=255)&($d
> >>12^$d>>4^$d^$d/8))<<17,$e=$e>>8^($t&($g=($q=$e>>14&7^$e)^$q*8^$q<<6))<<9
> ,$_=(map{$_%16or$t^=$c^=($m=(11,10,116,100,11,122,20,100)[$_/16%8])&110;$t
> ^=(72,@z=(64,72,$a^=12*($_%16-2?0:$m&17)),$b^=$_%64?12:0,@z)[$_%8]}(16..271))
> [$_]^(($h>>=8)+=$f+(~$g&$t))for@a[128..$#a]}print+x"C*",@a}';s/x/pack+/g;eval
> 
> On Wed, 20 Jun 2001, Eric J. Wisti wrote:
> 
> >
> > So, is this what you're looking for:
> >
> > #!/usr/local/bin/perl
> >
> > use warnings;
> > use strict;
> > my $item;
> > my %hash;
> >
> > #open(FILE, "file") or die "Can't open file: $!\n";
> > while (<DATA>) { # using builtin data
> >       # match line of at least 3 digits at beginning of line
> >       /^(\d{3})/; # or more visually $_ =~ /^(\d{3})/;
> >       $hash{$1} = "Offline"; # set $hash{matched char} = "Offline"
> > }
> >
> > print join("\n", keys %hash), "\n"; # print out 'keys' to verify
> >
> > __DATA__
> > 305 = normal,noselector,password,1-999
> > 307 = normal,noselector,password,1-999
> > 11307 = normal,noselector,password,1-999
> >
> > for each line of <DATA> (info below __DATA__ token; could be <FILE> with
> > open above while), match any line with at least 3 digits in the first
> > three characters and set $hash{digits} to "Offline". After executing the
> > above code, I get the following 'keys' from %hash:
> >
> > 305
> > 113
> > 307
> >
> > You might want to be more restrictive in your regex, if you don't want
> > something like 11307 to match.
> >
> > As for Learning Perl 3, http://www.ora.com/catalog/lperl3/ says that
> > "Learning Perl, 3rd Edition" is due out next month (July 2001).
> > http://www.bestbookbuys.com/books/compare/isbn/0596001320 says August
> > 2001. Maybe Randal can shed more light on the pub date as he is one of
> > the authors.
> >
> > FYI
> >
> > On Wed, 20 Jun 2001, Tom Yarrish wrote:
> >
> > > Date: Wed, 20 Jun 2001 13:51:13 -0500 (CDT)
> > > From: Tom Yarrish <[EMAIL PROTECTED]>
> > > To: [EMAIL PROTECTED]
> > > Subject: I feel like an idiot...
> > >
> > > Hey all,
> > > Well, the subject says it all.  Four days removed from a Perl class, and I
> > > feel like I'm forgetting everything (guess that's what happens when you
> > > don't take Randal's class ;p).
> > > Anyway, here's what I'm trying to do.  I have a file that I'm reading into
> > > the program with an open command.  Each line looks like the following:
> > > 305 = normal,noselector,password,1-999
> > >
> > > What I'm trying to do is take the first three digits from each line, and
> > > then use it as a key for a hash.  The value of each key will start out as
> > > "Offline".  The problem is that I can't remember how to get the digits
> > > into the hash as the key value.  Here's basically what I have so far:
> > >
> > > open(FILE, "file") or die "Can't open file: $!\n";
> > > while (<FILE>) { #I have <FILE> here instead of <> because I'll have
> > >                   another filehandle once I get this working
> > >   $item = /^\d{3}/; # Here's where I'm stuck
> > >   $hash{$item} = "Offline";
> > > }
> > >
> > > I know the $item = etc line is wrong, but I can't think of what it should
> > > be.  I don't want the entire line, just the three digits.
> > >
> > > Also, the 3rd Edition of Learning Perl isn't out yet correct?  Someone
> > > told me it was, but I didn't think it was.
> > >
> > > Thanks,
> > > Tom
> > >
> > > --
> > > #!/usr/bin/perl -w # 526-byte qrpff, Keith Winstein and Marc Horowitz
> > > <[EMAIL PROTECTED]> # MPEG 2 PS VOB file on stdin -> descrambled output
> > > on stdout # arguments: title key bytes in least to most-significant order
> > > $_='while(read+STDIN,$_,2048){$a=29;$c=142;if((@a=unx"C*",$_)[20]&48){$h=5;
> > > $_=unxb24,join"",@b=map{xB8,unxb8,chr($_^$a[--$h+84])}@ARGV;s/...$/1$&/;$d=
> > > unxV,xb25,$_;$b=73;$e=256|(ord$b[4])<<9|ord$b[3];$d=$d>>8^($f=($t=255)&($d
> > > >>12^$d>>4^$d^$d/8))<<17,$e=$e>>8^($t&($g=($q=$e>>14&7^$e)^$q*8^$q<<6))<<9
> > > ,$_=(map{$_%16or$t^=$c^=($m=(11,10,116,100,11,122,20,100)[$_/16%8])&110;$t
> > > ^=(72,@z=(64,72,$a^=12*($_%16-2?0:$m&17)),$b^=$_%64?12:0,@z)[$_%8]}(16..271))
> > > [$_]^(($h>>=8)+=$f+(~$g&$t))for@a[128..$#a]}print+x"C*",@a}';s/x/pack+/g;eval
> > >
> > >
> > >
> >
> >  Eric Wisti
> >  Kinetic, Inc.
> >  (651) 848-0477
> >
> >
> >
> 
> 
--
Today is Sweetmorn, the 25th day of Confusion in the YOLD 3167
Fnord.


Reply via email to