Help with Variable Scope, Regular Expressions

PERL

    Next

  • 1. Problem with perl module
    I have installed perl fresh from the source, and loaded some modules via -MCPAN. I am getting the following error message when I try to use Proc::ProcessTable. [iepm@socrates ~/v3src]$ kill-processes -p gnuplot Storable binary image v2.7 more recent than I am (v2.6) at .../../lib/Storable.pm (autosplit into ../../lib/auto/Storable/_retrieve.al) line 328, at /usr/lib/per l5/site_perl/5.8.7/i686-linux-thread-multi/Proc/ProcessTable.pm line 89 I use this on other systems, but cannot get it to work on this particular system. Any help would be appreciated. Connie Logg SLAC
  • 2. qx// vs File commands..
    Hello ... I have a list of about 100-200 Directories/Files/Files_with_wildcards... __DATA__ /var/log/messages* /var/backup/msgs* /home/rohan/configs/ /home/rohan/.gamer.dat and I need to pull the recent three of each of those files to pwd and rename them with timestamp (logging purposes).. so i plan to do the following for each of the file in my array... - check if the file/dir exists.. -e - get the most recent 3 backups of the file.. eg: messages.1 messages.2 etc.. (but by timestamp) - then copy the file to current directory... i did something like.. chdir "SimulationBaseDir"; or dir "Cant chdir to simu base dir"; foreach my $val(@{$FileContent{'FILE'}}){ my $file=$val->{'loc'}; #get the directory $file =~ s/\///; #remove the first "/" to look from sumilation base dir.. my $res=qx@ls -St $file|head -n3@; #get teh list of the latest 3 files from the location chomp $res; #remove \n // copy the file(S) now... #now copy file to current directoy.. } I am sure there are better ways of doing this... i would appricaite if i can get feedback.. and better ways of doing this.. sincerely, rohan my questions are as follows.. - what is faster/better(?) to check if each of those locations exist? qx/
  • 3. Transform column into row (correction)
    John Doe am Mittwoch, 18. Januar 2006 15.30: [...] > Another way (I don's say a better :-) ) could be not using a data > structure, but doing the transformation on the fly: > > - read a line of the input data > - record the first or a new field name while scanning (id001 etc.) > - if the field name is new / has changed, output it to a new file > - if it is not new / has not changed, append the value(s) > - repeat these steps open input file and new output file read a line of the input data and record the field name (id001 etc.) if the field name is new / has changed, output a newline (exept for the first line), the field name and the value. if it is not new / has not changed, output only the value. read next line from input file and repeat procedure close files joe
  • 4. Perl script to remove italic tags from bunch of xml files
    Hi All, I have to create perl script to remove <italic>test</italic> tags from xml files. Can anyone help me with this? Thanks, Shailesh
  • 5. Win32::TieRegistry problem
    Hello I have a problem with this module. My script does not work with all pcs . In fact when I execute it on my computer I have no problem but in an other one I have this error: *Can't call method "GetValue" on an undefined value* An extract of my script: $Registry->Delimiter("/"); $swKey= $Registry->{"LMachine/Software/Microsoft/Windows/CurrentVersion/Uninstall/"}; @ls= keys( %{$swKey} ); $p ='0'; %list_soft = (); foreach $entry (@ls ) { $Key= $Registry->{"LMachine/Software/Microsoft/Windows/CurrentVersion/Uninstall/$entry"}; *$valueString= $Key->GetValue("DisplayName");* $valueString=~s/ //g; $list_soft{$valueString} = $p; } Thanks for your help Oxx

Help with Variable Scope, Regular Expressions

Postby willmartell » Sat, 10 Apr 2004 02:15:14 GMT

ello All.

Thank you for reading my email :)
I am trying to get this code to write the variables in a specific order,
even if they are undefined or blank. I would like each row seperated by
newline. I am running into a problem with the scope of the variable being
limited to the block of code it is in.

Can anyone help.

Here is the code
[code]
#!/perl -w
use strict;

my $directory = "C:/documents and settings/administrator/my documents/";
my $file = "rentals April 6 2004 mesquite";
my $ext = ".txt";
my $outfile = "$file.parse";

open(FILE, "<$directory$file$ext") or die "Can't open $file \n\n$!";
#open(OUT, ">$directory$outfile") or die "Can't open $outfile \n\n$!";

while(<FILE>){

if(m/(\$+\d+)/g) {
my $price = $1;
$price =~ s/^\s+//; #remove leading spaces
$price =~ s/\s+$//; #remove trailing spaces
$price =~ s/(\d)\W(\d\d\d)/$1$2/;
print "\"\nprice: $price\",";

}

if(m/BD:*\s+(\d+)/g) {
my $bd = $1;
$bd =~ s/^\s+//; #remove leading spaces
$bd =~ s/\s+$//; #remove trailing spaces
print "\"beds: $bd\",";
}


if(m/BA:*\s+(\d+)/g) {
my $ba = $1;
$ba =~ s/^\s+//; #remove leading spaces
$ba =~ s/\s+$//; #remove trailing spaces
print "\"baths: $ba\",";
}


if(m/LA:*\s+(\d+)/g) {
my $la = $1;
$la =~ s/^\s+//; #remove leading spaces
$la =~ s/\s+$//; #remove trailing spaces
print "\"la: $la\",";
}

if(m/(\d\d\d-\d\d\d-\d\d\d\d)/g){
my $phone = $1;
print "\"phone: $phone\",";

}


if(m/(\d\w+\W\d\w+\W\d\s+)/g){
my $arrangement = $1;
print "\"arrangement: $arrangement\",";
# print ("\n");
}



if(m/(\w+[\W\s]?\w+[\W\s]?)\s+\W\s+(\w+\s\w+\s\w+\s\w+)\s+\W\s+(\d+\W\d+\W\d
+)/g){
my $location = $1;
my $paper = $2;
my $date = $3;
print "\"location: $location\",";
print "\"paper: $paper\",";
print "\"date: $date\",";
}


if(m/(\d\d\d+\s\w+[\W\s]?\w+)/g){
my $address = $1;

print "\"address: $address\",";
# print "\n";


}


}#end while

[/code]


Here is the result

price: $725","beds: 3","baths: 1","
price: $725","phone: 972-289-2098","location: Mesquite ","paper: The Dallas
Morning News","date: 04/08/2004","
price: $950","beds: 3","baths: 1","
price: $950","phone: 469-525-0204","address: 1620 Summitt","location:
Mesquite ","paper: The Dallas Morning News","date: 04/08/2004","
price: $1100","beds: 4","baths: 2","la: 2","
price: $1100","phone: 214-566-6106","location: Mesquite ","paper: The Dallas
Morning News","date: 04/08/2004","
price: $995","beds: 3","baths: 2","address: 995 mo","
price: $995","arrangement: 11/2ba/1 ","address: 995 mo","location: Mesquite
","paper: The Dallas Morning News","date: 04/08/2004","
price: $850","beds: 3","baths: 1","
price: $850","phone: 972-216-0999","address: 3621 Edgebrook-3bd","location:
Mesquite ","paper: The Dallas Morning News","date: 04/08/2004","
price: $995","beds: 4","baths: 1","
price: $995","phone: 972-303-5134","address: 3528 Statler","location:
Mesquite ","paper: The Dallas Morning News","date: 04/08/2004","beds:
4","baths: 2","phone: 214-356-2120","location: Mesquite ","paper: The Dallas
Morning News","date: 04/08/2004","beds: 3","baths: 1","phone:
214-417-6453","address: 2428 Vickie 214","location: Mesquite ","paper: The
Dallas Morning News","date: 04/08/2004","
price: $1050","beds: 3","baths: 2","arrangement: 3bd/2ba/2 ","
price: $1050","phone: 972-754

RE: Help with Variable Scope, Regular Expressions

Postby cclarkson » Sat, 10 Apr 2004 04:58:06 GMT


:
: I am trying to get this code to write the variables in
: a specific order, even if they are undefined or blank.
: I would like each row seperated by newline.

    What is a row? What specific order?


: I am running into a problem with the scope of the 
: variable being limited to the block of code it is in.
: 
: Can anyone help.

[snipped useful code and data]


    It looks like you want to extract certain fields
from certain lines of a text file. Once you have the
correct fields, what are you going to do with them?

    Another way to ask the question might be: What
data structure would be easiest for you to use when
the field extraction from the text file is complete?

    Here's a three ad example using an Array of
Hashes.

(
    {
        price           => 725,
        bedrooms        => 3,
        bathrooms       => 1,
        living_areas    => undef,
        phone           => '972-289-2098',
        address         => undef,
        location        => 'Mesquite',
        paper           => 'The Dallas Morning News',
        date            => '04/08/2004'
    },
    {
        price           => 950,
        bedrooms        => 3,
        bathrooms       => 1,
        living_areas    => undef,
        phone           => '469-525-0204',
        address         => '1620 Summitt',
        location        => 'Mesquite',
        paper           => 'The Dallas Morning News',
        date            => '04/08/2004',
    },
        price           => 1100,
        bedrooms        => 4,
        bathrooms       => 2,
        living_areas    => 2,
        phone           => '214-566-6106',
        location        => 'Mesquite',
        paper           => 'The Dallas Morning News',
        date            => '04/08/2004',
    {
)

    From this data structure I can sort on any field
(or multiple fields). I can present only certain
fields or do a search on a field.

    Don't worry about how we are going to convert the
text file to this data structure just concentrate on
what structure would be easiest for you to use and
tell us what it looks like.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328




Re: Help with Variable Scope, Regular Expressions

Postby willmartell » Sat, 10 Apr 2004 05:41:32 GMT

ello Mr. Clarkson,

When I run my code on the attached test data file, I get this.

[RESULT]
"
price: $825","beds: 3","baths: 1","
price: $825","phone: 214-821-2794","address: 2731 San Diego","location: Casa
View ","paper: The Dallas Morning News","date: 04/06/2004","
price: $800","beds: 3","baths: 1","
price: $800","phone: 214-502-9426","address: 2519 Bluffton","location: Casa
View ","paper: The Dallas Morning News","date: 04/05/2004","
price: $850","beds: 3","baths: 1","
price: $850","phone: 214-821-2794","address: 2731 San Diego","location: Casa
View ","paper: The Dallas Morning News","date: 04/05/2004","
price: $800","beds: 3","baths: 1","arrangement: 3bd/1ba/1 ","
price: $800","address: 11826 Strand","location: Casa View ","paper: The
Dallas Morning News","date: 04/04/2004","
price: $800","beds: 3","baths: 1","
price: $800","phone: 972-494-3178","address: 3178 or pgr","location: Casa
View ","paper: The Dallas Morning News","date: 04/04/2004","
price: $1195","beds: 3","baths: 2","
price: $1195","phone: 214-533-4554","address: 10622 Hermosa","location: Casa
View ","paper: The Dallas Morning News","date: 04/04/2004","
price: $975","beds: 4","baths: 2","
price: $975","phone: 469-569-0099","location: Casa View ","paper: The Dallas
Morning News","date: 04/04/2004",

[/RESULT]

My desired result is this, (I want to be able to import this data into
excel)

[DESIRED]
price: beds: bath: phone: address: location: paper: date:
data, data, data, data, data, data, data, data
[/DESIRED]

The problem is that I am getting duplicate matches with my regular
expressions and I am not getting the same fields on each record.
I would like to be able to import this data in excel from a csv text file.

HTH
William Martell

Thanks :)








----- Original Message -----
From: "Charles K. Clarkson" < XXXX@XXXXX.COM >
To: "'William Martell'" < XXXX@XXXXX.COM >; < XXXX@XXXXX.COM >
Sent: Thursday, April 08, 2004 2:58 PM
Subject: RE: Help with Variable Scope, Regular Expressions




RE: Help with Variable Scope, Regular Expressions

Postby willmartell » Sat, 17 Apr 2004 02:42:28 GMT

i Charles. Thank you very much for the code and the lesson. Works
perfectly! I am trying to add the address portion to this but I am still
having some trouble. I wanted to see if I could ask you questions regarding
your code for even more insight. Thanks in advance for your time and
consideration.



#!/usr/bin/perl

use strict;
use warnings;
#use diagnostics;

use Data::Dumper 'Dumper';


(OK, right here, it looks like you just made things easier by not naming a
hash and declaring the fields then printing via csv sub procedure, and
instead just
declared an anonymous hash, is this correct? )

# column names
print csv( {
date => 'Date',
price => 'Price',
bedrooms => 'Bedrooms',
bathrooms => 'Bathrooms',
living_areas => 'Living_areas',
phone => 'Phone',
address => 'Address',
location => 'Location',
arrangement => 'Arrangemant',
paper => 'Paper', } ), "\n";


my %ad;


while ( <DATA> ) {

if ( /^Price/ ) {

# add the last ad if it exists
print csv( \%ad ), "\n" if is_valid_ad( \%ad );



(Here I am still a little foggy, but I think you are saying that in the hash
%ad there are these keys and the default values in them are blank, if they
get replaced with
real values later in the program great, and if not, they will still print as
blank. Is this correct?)




# Set field defaults
%ad = (
date => '',
price => '',
bedrooms => '',
bathrooms => '',
living_areas => '',
phone => '',
address => '',
arrangement => '',
location => '',
paper => '',
);


(I am having trouble with this, I think you are using the array symbol
because we are dealing with a list, but that the values held in $_ and
returned by price_fields sub procedure will actually apply to the hash %ad,
Is this correct?? Another question is how does Perl now what $_ is
refering to, I guess the line that begins with Price but I am not sure,
please explain.)

# update %ad for this line
@ad{ qw| price bedrooms bathrooms living_areas | }
= price_fields( $_ );
}


if ( /^Home/ ) {
@ad{ qw| address arrangement | } = home_fields( $_ );
}

if (
m/
(\S+) # location ($1)
\s+-\s+
([^-]+) # paper with spaces ($2)
-\s+
(\d\d\/\d\d\/\d{4}) # date ($3)
/x
) {

# use hash slice to set fields
@ad{ qw| location paper date | } = ( $1, $2, $3 );

# trim trailing spaces in paper field
$ad{paper} =~ s/\s+$//;
}
}

print csv( \%ad ), "\n" if is_valid_ad( \%ad );

sub home_fields {
# This is a dummy function
return ( '', '' );
}

sub price_fields {

(what is happening when you write 'my $line = shift' ???)

my $line = shift;

# collapse all spaces
$line =~ s/\s//g;
my @fields = split /Price:|BD:|BA:|LA:/, $line;


(why is there an extra first field??)

RE: Help with Variable Scope, Regular Expressions

Postby cclarkson » Sat, 17 Apr 2004 06:18:20 GMT

illiam Martell < XXXX@XXXXX.COM > wrote:
:
: Hi Charles. Thank you very much for the code and the lesson.
: Works perfectly!

It wasn't meant to be a finished product. Just an example to
learn from. You need to, at least, add some error checking, but
I am glad it works. :)


: I am trying to add the address portion to this but I am still
: having some trouble. I wanted to see if I could ask you
: questions regarding your code for even more insight. Thanks
: in advance for your time and consideration.

No problem.
Please keep asking!


[ I edited the questions so they appeared under the code. ]

: # column names
: print csv( {
: date => 'Date',
: price => 'Price',
: bedrooms => 'Bedrooms',
: bathrooms => 'Bathrooms',
: living_areas => 'Living_areas',
: phone => 'Phone',
: address => 'Address',
: location => 'Location',
: arrangement => 'Arrangemant',
: paper => 'Paper', } ), "\n";
:
: (OK, right here, it looks like you just made things easier
: by not naming a hash and declaring the fields then printing
: via csv sub procedure, and instead just declared an
: anonymous hash, is this correct? )


Yes. An unnamed hash is called an anonymous hash.

I don't know if it is easier, but when I first started
Randal (I think?) said something about unnecessarily using
extra variables. Since, all this does is print out the
column names, it doesn't make sense to define a file scoped
variable for this one use.



:
: # Set field defaults
: %ad = (
: date => '',
: price => '',
: bedrooms => '',
: bathrooms => '',
: living_areas => '',
: phone => '',
: address => '',
: arrangement => '',
: location => '',
: paper => '',
: );
:
: (Here I am still a little foggy, but I think you are saying
: that in the hash %ad there are these keys and the default
: values in them are blank, if they get replaced with real
: values later in the program great, and if not, they will
: still print as blank. Is this correct?)

Yes. Though I would call "''", blank. Maybe an empty
or zero-length string, but blank sounds like it could have
spaces in it.






: while ( <DATA> ) {
:
: if ( /^Price/ ) {
:
: # add the last ad if it exists
: print csv( \%ad ), "\n" if is_valid_ad( \%ad );
.
.
.

: # update %ad for this line
:
: @ad{ qw| price bedrooms bathrooms living_areas | }
:
: (I am having trouble with this, I think you are using the
: array symbol because we are dealing with a list, but that
: the values held in $_ and returned by price_fields sub
: procedure will actually apply to the hash %ad,
: Is this correct??

Yes. It is called a slice. Read the section on slices in
'perldata'.

: = price_fields( $_ );
: }

: Another question is how does Perl know what $_ is
: referring to, I guess the line that begins with Price but
: I am not sure, please explain.)


while ( <DATA> ) {

is a short-cut for:

while ( defined( $_ = <DATA> ) )

Similar Threads:

1.Set a variable from a substring of another variable using Regular Expression

Howdy,

Here's my problem, I am reading in a file which contains lines of
different lengths.  I need to set the last word on lines which match a
pattern (contain 'dogs') as the value of another variable.


# My file contains some lines that do not have the word
# dogs in them; and I am only interested here in lines
# that contain the word "dogs"

$START="dogs are:";

# I have opened my file, and now each instance of $_
# represents an entire line in sequence.
# So long as there is a line - repeat this loop.

while( $_=<FILE> ) {
	$CURRENT_LINE=$_;
	$CURRENT_LINE_DUPE=$CURRENT_LINE;

# Above I have setup two variables to hold the value
# of the current line.
#
# If and only if the current line contains the phrase "dogs Are:"
# do I continue to work on this line.

          if ($CURRENT_LINE = /$START/) {

# What I want to do next is set the variable DOGSARE to the
# last word in this line.

		$DOGSARE=~ m|(\w$)|;
		print "$RDOGSARE";
		}
        }

In short - this doesn't work.  The problem is setting the DOGSARE
variable value; I cannot seem to get it to set.

Any help would be greatly appreciated.

Thanks in advance,

TC

2.C++ regular expression Vs. Perl regular Expression

3.Regular Expressions with Incremented Variable Embedded

4.Variable inside regular expression

Hi there,

Im trying to get specific groups out of the unix group file.  The list
of group names is contained in Group_list.txt, when I try and run this
code, I get no output.  Is it possible to use  variables inside regex?


----------------------------
#!/usr/bin/perl  -w

use Slurp;
$datafile="/home/mw/Group_list.txt";
$groupfile="/etc/group";
my @array = Slurp::to_array($datafile);

foreach $entry(@array) {

    open(FH, "<$groupfile");
    while (<FH>)
        {
        chomp $_ ;
        if ($_ =~ /^$entry/){print $_ . "\n" ;}
        }
    close (FH);
    }

----------------------------


Thanks in advance,  

matt

5.regular expression in a variable

I need to set a variable to a filename where only 1 section of the file is
static. 

 

For example:

 

$filename =~ /test/;

 

Where the following:

 

Print "$filename\n";

 

Would produce:

 

123test456.txt

 

The only way I see this being possible is with regular expressions but I
can't for the life of me figure out the syntax I need. 

 

Any help appreciated!

 

Curt

 

 

 


6. Using regular expression as a variable.

7. How to use backreferences in a variable for a regular expression

8. Variables as min,max in regular expression



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 76 guest