[Solved] How is a for loop written in Perl?

There are two types of for loop in perl, in addition to the for statement modifier. They look like this: # c-style for loop for ( my $i = 0; $i < 12; ++$i ) { … } # regular for loop for my $i (0..11) { … } # statement modifier … for 0..11; … Read more

[Solved] Grep certain files only

If this is for perl code, as your tags imply, then you would typically use grep: my @all = qw( 12345_lrg.jpg 12445_sml.jpg 14445_sml.jpg 12345_lrg.jpg 42345_lrg.jpg ); my @sml = grep /_sml\.jpg$/i, @all; 2 solved Grep certain files only

[Solved] Subtracting no of days with system date and converting to MMDDYYYY format in perl [closed]

I was close to closing this question as you didn’t include your workings. But I’ve seen your comment – you should edit your question to include that. So here’s your code: use POSIX ‘strftime’; print strftime “%Y_%m_%d_%H_%M_%S/n”, localtime() – 24 * 60 * 60); That’s pretty close. You have three small issues. You have /n … Read more

[Solved] Parse INI in Perl (list format)

You haven’t told us the expected format for the data or shown any existing code, so it’s impossible to know what you’re looking for, but this should get you at least 90% of the way there: use strict; use warnings; use Data::Dumper; my %config; my $group = ”; while (<DATA>) { chomp; next unless /\S/; … Read more

[Solved] Read file content and use Regular Expression to locate a pattern in each File in Perl

use strict; use warnings; use HTML::TreeBuilder::XPath; my ($dir) = @ARGV; my @files = glob “$dir/*”; for my $file (@files) { my $tree = HTML::TreeBuilder::XPath->new_from_file($file); my @opacity = $tree->findnodes_as_strings(‘//div[@class=”opacity description”]’); print “\n$file\n”; print ” $_\n” for @opacity; } solved Read file content and use Regular Expression to locate a pattern in each File in Perl

[Solved] Perl: string manipulation – surrounding a word with a character ‘@’

Whenever you need some reasonably common matching problem resolve in Perl, you should always first check the Regexp::Common family on CPAN. In this case: Regexp::Common::Email::Address. From POD Synopsys: use Regexp::Common qw[Email::Address]; use Email::Address; while (<>) { my (@found) = /($RE{Email}{Address})/g; my (@addrs) = map $_->address, Email::Address->parse(“@found”); print “X-Addresses: “, join(“, “, @addrs), “\n”; } 1 … Read more

[Solved] Perl: Compare 2 hash table values

@pataka : I am not printing twice but doing comparison for both alpha-numeric strings . So i have printed one for stings and another for numbers . We can do the same even in this way as shown below: #Same Key and Value foreach my $val1 (keys %hash1) { foreach my $val2 (keys %hash2) { … Read more

[Solved] perl grep with / on array

Perhaps the following will be helpful: use strict; use warnings; print “Interface Name,Vlan Id,IP Address\n”; while (<DATA>) { my $interface = /interfaces\s+(\S+)\s+/ ? $1 : q/”/; my $vlanID = /unit\s+(\S+)\s+/ ? $1 : q/”/; my $ip = /address\s+(\S+)\s+/ ? $1 : q/”/; print “$interface,$vlanID,$ip\n”; } __DATA__ set interfaces ge-1/0/0 unit 0 family inet address 10.100.200.1/24 … Read more