Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 24th, 2008, 10:02 AM
Anomander Anomander is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 1 Anomander User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 4 sec
Reputation Power: 0
Help!

Hi All,
newbie-ish question here. I have this script:

foreach $test (@num)
{
foreach $record (@neet)
{
if ($record =~ /$test/){
print "$record \n";
print $test;
}
}
}

For some reason, the $test variable doesn't seem to be carrying into the second "foreach" loop. Does anyone know why this is so? I suspect it might be an issue with the "$" character having a special meaning within REs. Please assist.

Reply With Quote
  #2  
Old April 24th, 2008, 10:34 AM
cowboyroy cowboyroy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 62 cowboyroy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 3 m 2 sec
Reputation Power: 2
im also a noob but this might work

perl Code:
Original - perl Code
  1. foreach (@num) {
  2. foreach (@neet){
  3. if ($record =~ /$test/){
  4. print "$record \n";
  5. print $test;
  6. }
  7. }}


(replacing the foreach with while might also work)

Reply With Quote
  #3  
Old April 24th, 2008, 02:34 PM
keath's Avatar
keath keath is offline
!~ /m$/
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: May 2004
Location: Leawood, Kansas
Posts: 2,513 keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level)keath User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 9 h 9 m
Reputation Power: 527
Anomander: Your code is fine, though there are more efficient methods, and you may not get the results you expect:
Code:
#!/usr/bin/perl
use strict;
use warnings;

my @num = qw/1 2 3 4/;
my @neet = qw/100 2 30 4/;

foreach my $test (@num) {
	foreach my $record (@neet) {
		if ($record =~ /$test/){
			print "$record\n";
		}
	}
}

since an open match isn't the same as equality:
Quote:
100
2
30
4

If the code doesn't match for you at all and it seems it should, perhaps you forgot to remove the line endings from data read from a file.

cowboyroy: the $record and $test variables are undefined in your code.

Reply With Quote
  #4  
Old April 28th, 2008, 02:25 AM
kalyanraj's Avatar
kalyanraj kalyanraj is offline
8 Miles
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: India
Posts: 286 kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level)kalyanraj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 19 h 50 m 34 sec
Reputation Power: 51
On the similar lines, you can read variable scoping in Perl

Variable Scoping

HTH,
Kalyan

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Help!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway