I am having problem with array and Hash data structure. Example is show
below:
#CODE 1
my @list;
my %this;
$this{x} = 1;
$this{y} = 2;
$this{z} = 3;
$this{Z} = 4;
Better (maybe):
my %this = (
x = 1,
y = 2,
z = 3,
Z = 4,
)
push @list, %this;
The last
I tried
@ans = split (/\r/s, $msg);
@ans = split (/\r/g, $msg);
@ans = split (/\r\n/s, $msg);
@ans = split (/\r\n/g, $msg);
2 things - Perl should handle the \r\n part for you - \n is normally a
match for whatever your OS's end of line marker is. You also don't need
the modifiers on the
Parsing English is hard ;- but if you're fairly confident of the
formatting, you can try and add in a marker and then split on that:
$msg =~ s/((?:JAN|FEB|...|OCT|NOV|DEC)\s\d+\s\-)/|#|$1/g;
@ans = grep { /\w/ } split('\|#\|', $msg);
The elipsis there is the rest of the months (note, they
is there a difference between
my @variable;
Creates a new, lexically scoped array. All the elements are undef.
my @variable=();
The same, though it assigns an empty list to the array, sort of an explicit
code comment This array is empty. Note, w/o the my this would empty an
existing array.
It might also be worth taking a look at slices, described in 'perldoc
perldata'.
It also *may* be worth rethinking your data model. Often (not always but
...) needing to insert something into a particular location in an array
implies a hash might be a better structure.
Just a thought.
a
I did a little googling and found Net::Telnet (built on top of IO::Socket)
saying:
In the input stream, each sequence of carriage return and line feed (i.e.
\015\012 or CR LF) is converted to \n. In the output stream, each
occurrence of \n is converted to a sequence of CR LF. See binmode() to
while () {#line1
print; #line2
}
But so that I could add some better control of this program, can anyone
please tell me what the ACTUAL VARIABLE name is that is being internally
used in ?? (line 1)?
And what ACTUAL VARIABLE name is being internally used in ?print;?
... but what exactly is the function called?
It's called a here doc - it's a unix/shell convention, allowing
multiline input:
$ mail root EOM
hi
mom
.
EOM
(old 'mail' ends w/ a '.' on line by itself)
perldoc -q HERE.Doc
Found in /usr/lib/perl5/5.8.0/pod/perlfaq4.pod
Why don?t
My goal is to automate this so that after specifying a folder, Perl will
pull the specified lines from each of the 25 txt files in that folder and
write them to a single output file.
If you don't need file names etc, you can just expand your pointies to use
a glob
@output = *.txt
but that
why I am unable to print the value of the main
package's variable (despite that I am using the fully qualified variable
name)?
Name main::var used only once: possible typo at C:\perlsrc\TEST.PL line
9.
Use of uninitialized value in concatenation (.) or string at
c:\perlsrc\TEST.PL line 9.
So, joyfully, I wrote the following:
use FileHandle;
foreach my $key (0 .. 34) {
$fh = new FileHandle VTOL$key;
$flist{$key} = $fh;
print $flist{$key} Header info\n;
}
But it will not compile, it errors with:
C:\VTOLparser.pl
String found where operator expected at C:\VTOLparser.pl
OS is found via the $^O (carrot capital O) special var:
$ perl -e 'print $^O'
linux
I think winx stuff starts w/ MS
C:\perl -e print $^O
MSWin32
but you don't really want to do that. File::Copy
$ perldoc File::Copy
use File::Copy;
copy(file1,file2) or die Copy failed: $!;
It's a windows cmd.exe thing. Only dbl quotes work for -e script
creation and there are a number of other issues. In general, one-liners
in cmd.exe aren't going to be very useful. You can get a different shell
(even bash) for winx but
a
Andy Bach
Systems Mangler
Internet: [EMAIL
$_ = shift @data until /Amount/; # start of transactions
and get this warning:
Use of uninitialized value in pattern match (m//) at CMS2AP.pl line 138.
One way to avoid the warning is to check for that first uninit'ed $_
#!perl -w
use strict;
my @data = qw(hi ho hee ha Amount hoo hooh);
2 ways of looking at your quesiton:
FindBin:
NAME
FindBin - Locate directory of original perl script
SYNOPSIS
use FindBin;
use lib $FindBin::Bin/../lib;
or
use FindBin qw($Bin);
use lib $Bin/../lib;
give you where the script itself is. The
Why don't I Get IP addresses in the IP Address column of the report
below. The data is there if I do a print after the split of the value
in %GAA_Up
foreach $GS_IPAddr (keys(%GAA_Up)) {
open (UP, $GS_Report) || die Could not open file
$GS_Report : $!\n;
The following code is live at http://www.biblescramble.com/Esther/x.cgi.
I want to put newlines where the tabs are in $insert= Hazel Work
states:
WYrelocate: No;
I've tried br, \n, etc. What do I need?
Not sure what you're after, exactly. Perl will replace tabs by:
$insert
Often I need to extract the file name from a path.
$current_file = c:/somedir/test.ps;
@split_path = split(/,$current_file);
$current_file = @split_path[-1];
print $current_file;
You want to use the '$' sigil here, not '@' (you're refering to just one
element, not the whole array):
Just keep doing it ;-
$tp3Lookup{$fileTypeKey}{$tpKey})
is your reference to the inner anon hash. Syntatic sugar pointers make it
(to me) a little clearer
$tp3Lookup{$fileTypeKey}-{$tpKey})
but
foreach my $anonKey ( keys %{
wwang suggested:
How about using split function? I mean something like:
my @myCounts = split @YourData, /;/
print $#myCounts;
Er, close - your split is reversed and you want to split the string, not
an array:
my @myCounts = split /;/, $YourData;
but this points to an interesting thing we just
The match condition is number of ; in 1 line
Output:
found 14 ; in 1,232 lines
found 13 ; in 7,456 lines
found 12 ; in 2,321 lines
my %line_counts;
while () {
my $semis = tr/;/;/;
if ( $semis ) {
$line_counts{$semis}++;
}
else {
warn(Bad data - no semis: $_);
}
Not sure I'm getting it completely, but using match in a while loop w/ the
/g modifier lets you process a string one match at a time:
my $string = Lots of words to be read one at a time.\nthough more than
one line;
while ( $string =~ /(\w+)/g ) {
print Found: $1\n;
print Proceessing ...\n;
To add a zero pad (printf if you just want it printed zero padded):
my $padded_name = sprintf(%02d, $month);
the 'zero' is the pad char, the '2' is the number of places to pad to
To get rid of it:
print The month number is: , $padded_name + 0, \n;
that is, use it as an integer and Perl'll
... called quines ...
And, in perl, one of the most amazing quine programs is Damian Conway's
SelfGol. It's also the game of Life and a quine-izer for other programs -
don't even think about looking at it.
http://libarynth.f0.am/cgi-bin/twiki/view/Libarynth/SelfGOL
I had the good fortune to
Try:
#!/usr/bin/perl -w
use strict;
$_ = ABabcde12345BAABabcde1234blahblah5BA;
print is: , $_ , \n;
my $count = () = $_=~ /(AB.*?BA)/g;
my @match = /(AB.*?BA)/g;
print match: , join(, , @match), \n;
print I matched $count times\n;
The trick is:
$_ =~ /../
applies the
Just a small point - the shebang on winx does help perl - it will use any
command line options (e.g
#!perl -Tw
turns on taint and warnings) it finds in the shebang, regardless of the
path.
a
Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738 FAX 264-5932
Call out
$Bill wrote:
... and \1 is deprecated for $1.
I believe that should read [thanks Greg!] ...and \1 is deprecated on the
right
side of s///. On the left side (as in any regexp), \1 and $1 differ. But
as a holdover from sed, \1 means the same as
$1 on the right side of the subst.
\1 isn't the
A further debug/syntax nitpick:
print foo='$foo'\n;
$foo =~ /([^.]+).txt/;
$ans = $1;
print ans='$ans'\n;
best as:
my ($foo, $ans);
print foo='$foo'\n;
if ( $foo =~ /([^.]+)\.txt/ ) {
$ans = $1;
} else {
$ans=match failed on: $foo;
}
print ans='$ans'\n;
if you check your
One guess:
My intiial thought is that i need to see more info. My guess is that the
mason
interpreter isn't even getting those requests. I would try setting it up
to
use an HTML::Mason::CGIHandler object. There should be an example at
http://www.masonhq.com/
a
Andy Bach, Sys. Mangler
I don't understand this line:
$ips{$source_ip}-{$dest_ip}++;
how can you access and increment something that doesnt exist?
Fancy name: autovivification - perl, w/ your best interests at heart
(generally) knows what you want to do, so it automagically creates the
source_ip hash entry (if it
Because your -5 hours off of epoch time - i.e.:my ($secs, $mins, $hours, $mday, $mon, $year, undef, undef, undef) =localtime($endTime - $startTime);print "Elapsed time was $mon/$mday/$year - $hours:$mins:$secs\n";gives:Elapsed time was 11/31/69 - 18:0:10That isplus 10 seconds minus (in my case) 6
perldoc Net::FTP:
dir ( [ DIR ] )
Get a directory listing of DIR, or the current
directory in long format.
In an array context, returns a list of lines returned
from the server. In a scalar context, returns a
reference to a list.
so, for Solaris
You might do better to make an altenation:
$keywords = join(|, @keywords);
if ( $wholefile =~ /^\s*($keywords)\s*$/mo ) { # do you want 'i' ?
rather than the loop. Worth a benchmark, but one search for multiple
matches is probably faster than multiple searches for some sized files.
You also
Works fine here - 'course the '.' in Wile E. is going to match any char
so:
Wile E. Coyote
matches
Wile Ej Coyote
matches
Wile E. Coyotexxx
matches
using quotemeta or \Q:
#my $var = coyote;
my $var = quotemeta Wile E. Coyote;
while (DATA) {
if ( /$var/oi) {
print $_ . matches\n;
}
Isn't it a little silly to go on to try and read from whichever file
handle after the open has failed? You've got two flags set (errorcheck2
and error_flag), so at least use one to skip around the while .
It looks like you're expecting to have a number of possible failures -
make sure to
Perl uses the line ending appropriate for the platform \r\n for dos \n for
*nix and translates the
print hey\n
appropriately. You can do:
$var =~ s/\r//;
to remove the ^M but you probably want to look into setting the var $/ and
use chomp.
perldoc -f chomp
chomp This safer version of
one way:
sort { $a +0 = $b + 0 } @msgs
forces them to be treated as numbers, not strings. Hmm:
@msglist = sort { $a = $b } @msgs;
$found = $msglist[0];
for ($i = 0; $i scalar(@msglist); ++$i) {
print Checking message $msgs[$i]\n;
last if ($msglist[$i] == $message);
}
$found =
Its rude/bad/silly to post msgs to two lists at once, esp. as this is _javascript_ not perl but it appears to be a wanker's code to get folks to a web site in the Netherlands. All that:function rd() { var a = l3[4]+l1[8]+l2[4]+l1[3]+l2[5]+l3[4]; var b =
print `cd`;
(those are back tics, not quotes) works. cd by itself, in winx returns
the current dir.
a
Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738 FAX 264-5030
We are either doing something, or we are not. 'Talking about' is a
subset of 'not'.--
My Sharp Zaurus 5600 runs perl.
a
Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED]
VOICE: (608) 261-5738 FAX 264-5030
We are either doing something, or we are not. 'Talking about' is a
subset of 'not'.-- Mike Sphar in alt.sysadmin.recovery
40 matches
Mail list logo