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

Hi, Im hopeless at perl and desperately need some help. I need my program to take two arguments at execution. It needs to open the file named as the second argument, read it line by line splitting the each line at the colon and checking the left hand side of the split for any instances of the first argument. If it finds it, it prints it out. This is what ive got so far: (the name of the file it checks through is called groupinfo)
#!/usr/local/bin/perl %groupinfo open(IN, "groupinfo") or die "Can't open groupinfo: $!"; while (<IN>) { chomp($line); ($name, $desc) = split /:/, $line; if (/$line/){ $groupinfo{$name} = $desc; } } print "%desc";
i cant even get it to compile and have no idea what im doing wrong. e.g so say if groupinfo looks like this:-
aaaa:bbbb cccc:dddd
and you run it:-
[xxx]$ perl groupinfo.pl aaaa groupinfo
it will print:-
bbbb
Any help would be appreciated, thanks.

20040421 Edit by BazB: Changed title from 'need help with perl'

Replies are listed 'Best First'.
Re: How do I use command line arguments?
by Belgarion (Chaplain) on Apr 20, 2004 at 22:04 UTC

    Well your code would not pass strictness as it looks now, but we'll ignore that for the moment. Do you want to print out the whole line that matches, or just the right hand side? The code below only prints the right hand side.

    open(IN, $ARGV[1]) or die "Can't open groupinfo: $!"; while (<IN>) { chomp; my ($name, $desc) = split /:/, $_, 2; next unless $name eq $ARGV[0]; print $desc; } close(IN);

    You had the right idea, but you had your print in the wrong place, and there really isn't any need for the %groupinfo hash if you just need to print the results.

    The @ARGV array holds the arguments passed to the perl script.

Re: How do I use command line arguments?
by shemp (Deacon) on Apr 20, 2004 at 22:29 UTC
    Just to deal with the part of reading command line arguements, as was pointed out, they are stored in @ARGV, i.e.
    ... if ( @ARGV != 2 ) { # check that you have the proper number of args print "usage: $0 pattern infile_name\n"; exit; } my ($pattern, $infile_name) = @ARGV; ...
    I generally like to check that i have the proper command line args before i do anything else, although this checking is often more complicated.
Re: How do I use command line arguments?
by BUU (Prior) on Apr 21, 2004 at 04:34 UTC
    Why do you have a random "%groupinfo"? That line should end with a semi-colon and start with a "my" keyword. my %groupinfo;

    Secondly the only place you mention desc is in the body of the while loop, and it's a scalar. Why are you trying to print out a hash named desc later on?

     $desc = "foo"; Does not affect anything named %desc.

    Where do you get the variable $line from? You don't declare it. You don't assign anything to it. You just made it up and hoped it worked?

    Heres a rewrite that sucks slightly less:
    #!/usr/bin/perl use strict; # or die my %groupinfo; open(IN, "groupinfo") or die "Can't open groupinfo: $!"; while( my $line = <IN> ) { my( $name, $desc ) = split/:/,$line; $groupinfo{$name} = $desc; } print %groupinfo;