RE: Simple Split Question
I don't think you want to use split.. at least I wouldn't. I would do: my %foo = (); my $line = 'DimView 1 "All" DimView 2 "Some" DimView 3 "Most" DimView 4 "None"'; $foo{$1} = $2 while $line =~ m/DimView (\d) "([^"]+)"/g; That gives me %foo as (according to Data::Dumper): { 1 => 'All', 2 => 'Some', 3 => 'Most', 4 => 'None' }; > -Original Message- > From: Seitz, Scott [mailto:[EMAIL PROTECTED]] > Sent: Thursday, June 28, 2001 1:32 PM > To: '[EMAIL PROTECTED]' > Subject: Simple Split Question > > > I'm having trouble with what I think is a very simple split question. > > I've got a line of text something like: > > DimView 1 "All" DimView 2 "Some" DimView 3 "Most" DimView 4 "None" > > I want a hash with (1, "All", 2, "Some", 3, "Most", 4, "None") > > I'm spitting on /DimView/, but I can't get the syntax to throw it into the > hash correct. Can one of you thow me a couple of lines of code to get me > through this? > > Thanks, > > Scott > - Stephen Nelson "stephen" on PerlMonks (www.perlmonks.com) [EMAIL PROTECTED]
RE: using strict
Fortunately or unfortunately, you can't print to files just by using $file as a filehandle. You need to open the file first. foreach $k (sort keys (%all_genes)) { for (1..5){ if ($k =~ /[$_]g/){ $file = "CHR$_"; open(FILE, "> $file") or die "Couldn't open '$file': $!; stopped"; print FILE ">$k\t$all_genes{$k}\n"; print ">$k \t $all_genes{$k}\n"; close(FILE); last; } That's if you want your files to be CHR1, CHR2, etc... > -Original Message- > From: Hans Holtan [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, June 27, 2001 4:36 PM > To: [EMAIL PROTECTED] > Subject: using strict > > > Hi everyone, > > I get this error when I run my script, and I don't understand why: > > "Can't use string ("CHR3") as a symbol ref while "strict refs" in use > at AtIntergenicTableSort.pl line 53. " > > I'm trying to split my output into 5 seperate files based on what is > in the key. Here is a snip of code: > > #Sort the hash and write to a new file > foreach $k (sort keys (%all_genes)) { > for (1..5){ > if ($k =~ /[$_]g/){ > $file = "CHR$_"; > print $file ">$k\t$all_genes{$k}\n"; > print ">$k \t $all_genes{$k}\n"; > last; > } > >
RE: checking groups on unix
Since you're doing a string compare on $groupname, you need to use 'ne', not '!='. foreach my $file (@files) { #getgrpid returns the group file entry for a given group id. my $groupname = (getgrgid((stat($file))[5]))[0]; if ($groupname ne "groupname") { print "$file has bad groupname: $groupname\n"; } } (since "foo" == "bar" numerically) > -Original Message- > From: Chas Owens [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, June 27, 2001 3:11 AM > To: [EMAIL PROTECTED] > Subject: Re: checking groups on unix > > > > How to build @files is left as an exercise for the reader. > > > foreach my $file (@files) { > #getgrpid returns the group file entry for a given group id. > my $groupname = (getgrgid((stat($file))[5]))[0]; > if ($groupname != "groupname") { > print "$file has bad groupname: $groupname\n"; > } > } > > > > On 27 Jun 2001 09:59:07 +0100, PURMONEN, Joni wrote: > > Hi ya, > > > > I need to check the group status on numerous files/directories, > and haven't > > been able to fing out the best way to do it with perl. I simply > need to see > > if some directories do not have certain group set on them. > > > > Can anyone give any pointers? > > > > Cheers, > > > > Joni > > > > Ps. I only have learning perl and some other fairly simple books which > > didn't seem to have anything useful in them > > > -- > Today is Pungenday, the 32nd day of Confusion in the YOLD 3167 > This statement is false. > > >
RE: Re[2]: A better way?
Actually, calling a subroutine with an ampersand and no parens does not call the subroutine with no arguments. (To make it clear, though, I AGREE with Jos I. Boumans' larger point, just correcting a smaller point that I think makes his point even more clearly...) Using the ampersand without passing arguments causes a subroutine to be called with the same arguments as its calling subroutine. So &foo called from bar('a', 'b') does the same thing as foo('a', 'b'). Say I've got sub foo, which is supposed to just print out a list of things passed to it, and print an "empty list" message otherwise: sub foo { if (@_) { print "Output: ", @_, "\n"; } else { print "Output: empty list\n"; } } Okay, so next I have a subroutine bar(), which calls foo twice: sub bar { &foo; foo(); } Then, let's say in the main program I call bar() with arguments: bar('a', 'b'); What will the output be? Well, since you're not explicitly passing anything to foo() in either case, you'd expect it to be: Output: empty list Output: empty list BUT IT ISN'T. You get: Output: ab Output: empty list I find this a good reason to avoid the & syntax, or to at least always use parens. > -Original Message- > From: Jos I. Boumans [mailto:[EMAIL PROTECTED]] > Sent: Saturday, June 23, 2001 2:33 AM > To: iansmith; [EMAIL PROTECTED] > Subject: Re: Re[2]: A better way? > > > Ehm, you cant *just* use & and expect all to stay the same... > concider the following: > > if you use a & you dont need to predeclare a sub if you want to > leave of the > parenthesis for an argument list: > ie: > &foo#calls sub foo with no arguments > foo #calls sub foo with no arguments IF you predeclared sub foo > anywhere... if not, its' a bareword > > also, and this is more important, if you set up prototyping for > your subs, & > will disable prototype checking... > now most of the time you dont set up prototypes so it's unlikely you get > bitten, but the second you change your sub to use prototype's and > you do not > leave off the & in your sub calls, they wont get checked > > read perldoc perlsub for more information > > hope this clears things up, > > Jos Boumans > > > > > > I am using the most recent from ActiveState, but I tend to stick the & > > > out there so I can tell at a glance that I am calling a sub. Does it > > > cause problems or is it just not needed? > > > > It doesn't cause any problems that I am aware of. Go ahead and use > > it if it makes subroutines stick out. > >
RE: array slice question
Well, if you're OK with printing the colons in the last field, you could simply say: print ( ( split(/:/, $_, 9) )[1,6,8] ); Since that'll absorb all of the ending fields into the ninth (from zero, so index 8) field. That would be the correct thing to do if the colons in the ending field are not field delimiters, but just part of the text. Other than that, you may need to bite the bullet and name the array. > -Original Message- > From: Bradford Ritchie [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, June 26, 2001 1:46 PM > To: [EMAIL PROTECTED] > Subject: array slice question > > > Hi, > > I have an unnamed array which I created from splitting up a colon > separated string: > > $_ = "0th:1st:2nd:3rd:4th:5th:6th:7th:Some random text: > might have :colons: or might not" > print ((split /:/)[1,6,8]); > > ...but I really need to print everything after the 8th element. > If the array were named, I could do something like this: > > @arr = split(/:/); > print @arr[1,6,8..$#arr]); > > ... and this would include everything after the 8th array > element. I know that -1 should start from the end of the array, > but specifying [1,6,8..-1] doesn't work. > > I didn't see anything in perldata about it so I'm hoping someone > has a solution. > > Thanks. > -- Brad > >
RE: environment-Variable from korn-schell to perl
You can execute the korn-shell script internally by using system(). ## The perl script use strict; $ENV{'PASSED_FROM_PERL'} = 'Hello, ksh!'; system('ksh', 'kornscript.sh') == 0 or die "system returned ", $? << 8, "from ksh kornscript.sh; stopped"; - ## kornscript.sh echo "Passed from Perl: $PASSED_FROM_PERL" Prints out: Passed from Perl: Hello, ksh! For more details, do a 'perldoc -f system'. > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: Monday, June 25, 2001 4:18 AM > To: [EMAIL PROTECTED] > Subject: environment-Variable from korn-schell to perl > > > Hi, > I have to source a KORN-Shell Script, so that the environment-Variables > created by > KORN-Shell is available in my PERL-Script. Ho do I that > It is vorbidden for me to manipulate / modify the KORN-Shell Sripts. > Thank's > > Peter Schopen > RZF NRW - Sachgebiet 215 > Postfach 30 08 64, 40408 Düsseldorf > Tel. (02 11) 45 72- 859; Fax (02 11) 47833-065 > E-Mail-Adresse: [EMAIL PROTECTED] > > >