Re: why does perl critic not like @_
I've never seen people using @_ as some kind of a temporary variable and I think this practice should be discouraged. Probably Perl::Critic should have a policy finding and reporting such cases as this is just confusing. @_ is a special variable to receive function parameters. Gabor http://perlmaven.com/ ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: limiting keys(%hash)
On Fri, Jun 15, 2012 at 10:29 AM, Rothenmaier, Deane wrote: > Yes, it’s Yet Another Noob Question, but the Camel stands mute… > > > > Is there a way of limiting keys()? I want to change some values in a hash, > the values to change or not depending on the value of their keys. > > > > I.e.: > > my %hash = ( 1 => “first”, > > 2 => “second”, > > 3 => “third”, > > 4 => “fourth”,); > > > > and I want to change the values for 2 and 3 to “zweite” and “dritte”. > > > > The actual (sort of) line I’m working on is: > > > > map { $florida_keys{$_} =~ s{/Fred/}{/Barney/} if $_ >= 124 } keys( > %florida_keys ); I am not sure if this is what you meant, but have you tried grep? map { $florida_keys{$_} =~ s{/Fred/}{/Barney/} grep { $_ >= 124 } keys( %florida_keys ); Check http://perldoc.perl.org/functions/grep.html regards Gabor -- Gabor Szabo http://szabgab.com/ Perl Maven http://perlmaven.com/ Perl Weekly http://perlweekly.com/ ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Daylight Savings Time?
On Thu, Jan 26, 2012 at 10:27 PM, Barry Brevik wrote: > I'm on Windows Perl 5.8.8 and I'm writing an app that needs to deal with > daylight savings time. The date is an RFC 822 formatted date which looks > like this "Wed, 03 Sep 2008 08:58:27 -0800", and what I'm looking for is > the "-0800" part which changes based on the daylight savings time state. > > I want something light weight (rather not use a module) and it only has > to deal with the PST timezone. > > Has anyone found a Win32:: method that will retrieve the DST state from > Windows? Also, when the date is expressed in that format, is it supposed > to represent UTC time along with the offset? > > Barry Brevik While I think I'd stromgly recommend using the DateTime modules for anything except the most basic time handling you could use localtime(). In list context it returns the DST state as the last value so (localtime())[-1] would give that value. AFAIK that works on Windows as well. regards Gabor -- Gabor Szabo http://szabgab.com/ ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Problem with regex
Hi Barry, On Thu, Nov 10, 2011 at 2:34 AM, Barry Brevik wrote: > Below is some test code that will be used in a larger program. > > In the code below I have a regular expression who's intent is to look > for " <1 or more characters> , <1 or more characters> " and replace the > comma with |. (the white space is just for clarity). > > IAC, the regex works, that is, it matches, but it only replaces the > final match. I have just re-read the camel book section on regexes and > have tried many variations, but apparently I'm too close to it to see > what must be a simple answer. > > BTW, if you guys think I'm posting too often, please say so. > > Barry Brevik > > use strict; > use warnings; > > my $csvLine = qq| "col , 1" , col___'2' , col-3, "col,4"|; > > print "before comma substitution: $csvLine\n\n"; > > $csvLine =~ s/(\x22.+),(.+\x22)/$1|$2/s; > > print "after comma substitution.: $csvLine\n\n"; > Tobias already gave you a solution and I also think using Text::CSV or Text::CSV_XS is way better for this task thank plain regexes, For example one day you might encounter a line that has an embedded " escaped using \. Then even if your regex worked earlier this can kill it. And what if there was an | in the original string? Nevertheless let me also try to explain the issue that you had with the regex as this can come up in other situations. First, I'd probably use plain " instead of \x22 as that will be probably easier to the reader to know what are you looking for. Second, the /s has probably no value at the end. That only changes the behavior of . to also match newlines.If you don't have newlines in your string (e.g. because you are processing a file line by line) then the /s has no effect. That makes this expression: $csvLine =~ s/(".+),(.+")/$1|$2/; Then, before going on you need to check what does this really match so I replaced the above with if ($csvLine =~ s/(".+),(.+")/$1|$2/s ){ print "match: <$1><$2>\n"; } and got match: <"col , 1" , col___'2' , col-3, "col><4"> You see, the .+ is greedy, it match from the first " as much as it could. You'd be better of telling it to match as little as possible by adding an extra ? after the quantifier. if ($csvLine =~ /(".+?),(.+?")/ ){ print "match: <$1><$2>\n"; } prints this: match: <"col >< 1"> Finally you need to do the substitution globally, so not only once but as many times as possible: $csvLine =~ s/(".+?),(.+?")/$1|$2/g; And the output is after comma substitution.: "col | 1" , col___'2' , col-3, "col|4" But again, for CSV files that can have embedded, it is better to use one of the real CSV parsers. regards Gabor -- Gabor Szabo http://szabgab.com/perl_tutorial.html ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: How to push a hash on to a hash of array entries
On Tue, Nov 8, 2011 at 7:47 PM, Paul Rousseau wrote: > I am getting closer, Gabor. I am glad to hear that :) > > I had the wrong index. These two lines > > push @{$project{$projectno}}, \%days; > print Dumper %{$project{$projectno}[3]}; I think it is better to always give a reference as a parameter to Dumper (put the back-slash in front of the %) > Now to get at the description within the %days hash. This next line prints a > count of the array pointed at by the '3' key. > > print "test 1 " . @{${$project{$projectno}[3]}{"3"}}; > > prints "test 1 2" > It looks too complex to me. I wonder if you could change the strategy and work with smaller structure. That will be easier to comprehend. Leaving spaces inside the brackets can also help. It also seems you have an extra, unnecessary pair of curly braces: This might be slightly more readable: @{ $project{ $projectno }[3]{3} } regards Gabor Perl Tutorial http://szabgab.com/perl_tutorial.html ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: How to push a hash on to a hash of array entries
Hi Paul, there are a couple of issues I could see in the code you sent, let me point out some of them and hope those will help. In general I'd recommend using the Dumper function of Data::Dumper to check what kind of data structure you got. On Tue, Nov 8, 2011 at 1:41 AM, Paul Rousseau wrote: > Hello Perl Community, > > I want to build a complex hash. > > I want my hash structure to look like this: > > # %project = ( => [, > # , > # %days1 = ( => > [,], > # => > [, ] > # ) > # ], > # , > # , > # %days2 = ( => > [,], > # => > [, ] > # ) > # ] > # ) > > I am having trouble building up and populating the individual %days hash. > > Here is what I have so far. > > use strict; I'd recommend adding "use warnings;" as well. > > my ( > %project, > $projectno, $desc, > $day, > %days, > $i > ); > > $projectno = "12345"; > "$desc = "testing first project"; > > $project{$projectno} = [$desc, "0"]; # zero hours > > #later on, my code reads in some records such as > > # 3, 12, "painted a room" > # 5, 6, "added a chair" > # 14, 2, "made some calls" > > # for, say, project "12345" > > For my first test, I populated a smaller hash called %days. > > for ($i = 1; $i <=3; $i++) > { > ($day, $hour, $text) = split (',', ); > $days{$day} = ($hour, $text); > } > If I am not mistaken you want to pass an array reference there and not an array so need square brackets: $days{$day} = [$hour, $text]; Try this: use Data::Dumper qw(Dumper); print Dumper \%days; > # > # now that I have finished populating a smaller hash, I want to copy the > hash on to the bigger hash. > # > > push @{$project{$projectno}}, %days; In this case perl will flatten the hash and you will push all the keys and values of it to the array. Try printing out the result with the Dumper. I am almost sure you will want to write: push @{$project{$projectno}}, \%days; See the back-slash in-front of the hash. regards Gabor -- Gabor Szabo Perl Tutorial: http://szabgab.com/perl_tutorial.html Perl Weekly: http://perlweekly.com/ ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: regular expression, this but not that
On Mon, Sep 19, 2011 at 7:38 AM, Jer A wrote: > All, > > lets say i want to test if a string contains "pig" but not "dog" and/or not > "cat" > > i tried something like this: > > =~ m/[^dog]|[^cat]|pig/ig > > what is the best way of going about this, using one regex? > > your help is very much appreciated, > thanks. > Hi Jeremy, I am not sure why would you want to use only one regex and IMHO it would be very complex to write that. It would make your code both unreadable and slow. I'd use two regexes for that: if ($str =~ m/pig/ and $str !~ /dog|cat/) { } Add the railing /i if you want this to be case insensitive. The /g is not needed as the first pig will be ok and the first dog or cat would also be indication that the match should fail. regards Gabor -- Gabor Szabo http://szabgab.com/ ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Poll: Which editor(s) or IDE(s) are you using for Perl development?
Hi, I have setup a simple 5-second poll to find out what editor(s) or IDE(s) people use for Perl development. I'd appreciate very much if you clicked on the link and answered the question. You can mark up to 3 answers. Please also forward this mail in the company you are working and to people in your previous company so we can get a large and diverse set of responses. The poll will be closed within a week. Please act now! http://bit.ly/perleditorpoll thanks Gabor http://szabgab.com/blog.html http://perlmongers.wordpress.com/ ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Need help with printf/sprintf
On Sat, Aug 29, 2009 at 3:51 PM, Daniel Burgaud wrote: > Hi > > I have a problem with printf/sprintf > > Basically I have this code: > printf "%-50s %10.2f\n", $item, $price; > > This code prints a line that is 61 chars long with the Item's name on the > Left and Price on the right. > However, I want to make it variable width based on either screen width, or > user input: > ie, > > print "%-$Ws %10.2f\n", $item, $price; > > Obviously, this does not work. How do I code it to work like i want it? printf "%-${W}s %10.2f\n", $item, $price; Gabor -- Gabor Szabo http://szabgab.com/blog.html Perl Training in Israel http://www.pti.co.il/ Test Automation Tipshttp://szabgab.com/test_automation_tips.html Perl 6 Tricks and Treats http://szabgab.com/perl6_tricks_and_treats.html ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: need help from a new hand for perl
On Tue, Aug 25, 2009 at 1:08 PM, Chris Wagner wrote: > At 03:43 PM 8/19/2009 -0500, Fei Shi wrote: >>Only when I run the code below I have a little problem: >>*cat ids_overlap_* | sort -u > "a file name"* >> >>I have 3 files named as *ids_overlap_*(21Us/coding/ram)*. The error message >>is: >>*The system cannot find the file specified. >>cat.exe: write error : invalid argument* > > > Why are there star characters there? If u are trying to run that line from > within a perl script it won't work. > > system 'cat ids_overlap_* | sort -u > "a file name"'; > or > @return = `cat ids_overlap_* | sort -u > "a file name"`; > I have not seen the previous posts but if you are trying to run this on windows it won't as you are trying to invoke a unix-is shell command. Maybe under Cygwin. I don't know what are you trying to achieve but I guess the next line is reading the file "a file name" so you will have the list of ids_overlap_* files in an array. It would be easier to use the glob() function of perl: @return = glob "ids_overlap_*"; and if you want them sorted then this will do it: @return = sort glob "ids_overlap_*"; Gabor http://szabgab.com/blog.html ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: use case for activeState
On Mon, Jun 22, 2009 at 1:12 PM, Angelos Karageorgiou wrote: >Well I have a windows desktop auditor all in Activeperl together with the >registry helper. The Gui designers are a bit outdated but >the speed of >deploying these products is amazing. > http://sourceforge.net/projects/rautor > Chris Wagner wrote: >> >> Not to be melodramatic, but u can create the entire Internet using >> ActiveState Perl. U could build Yahoo, Google, and Amazon for starters. >> It's really suitable for every conceivable task other than audio video. >> > > Note entirely true ... > > I have a tiny win32 microphone recorder application written entirely in Perl > ! Zip file included ! Gui was developed with activestate's guibluider. > > Beware , buttons legends are in Greek, adjust accordingly, and oh, released > under GPLv2 :-) Angelos, it probably only reflects on my ignorance but I have never heard of either of your projects. I am not sure if you know about the Iron Man Blogging Challenge http://ironman.enlightenedperl.org/ trying to promote Perl by blogging about related subjects. One thing I am really missing from there is the description/promotions of many tools written in Perl that don't necessarily need knowledge of Perl to use. http://szabgab.com/blog/2009/06/1245788806.html I think it would be interesting to read about your projects as well. regards Gabor ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: How to query a website via Perl
On Tue, May 12, 2009 at 4:35 PM, steve silvers wrote: > > Can someone please so me how to query a website remotely via cgi. > > Something like > > http://somesite/query=$var > > Then return the results. > > Thanks in advance Look at these modules on CPAN http://search.cpan.org/ LWP::Simple LWP WWW::Mechanize Gabor -- Gabor Szabo http://szabgab.com/blog.html Perl Training in Israel http://www.pti.co.il/ Test Automation Tipshttp://szabgab.com/test_automation_tips.html ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Perl on Windows use cases
* I've asked it on the Vanilla mailing list already but it might be interesting * to see what people over here are thinking about the subject. * http://win32.perl.org/wiki/index.php?title=Win32_Mailing_Lists * http://www.mail-archive.com/[EMAIL PROTECTED]/msg00071.html Hi, It seems most of the Perl developers are using some Unix flavor (including Linux, *BSD and even Mac). Probably not on this list though. I'd like to have an idea What do you use Perl for on Windows? In addition I'd like to know what is the single biggest thing you are missing from Perl on Windows? Please respond here or on use.perl.org http://use.perl.org/~gabor/journal/37947 or some other place I might find. thanks Gabor -- Gabor Szabo http://szabgab.com/blog.html Perl Training in Israel http://www.pti.co.il/ Test Automation Tipshttp://szabgab.com/test_automation_tips.html ___ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs