472,785 Members | 1,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,785 software developers and data experts.

split commands oddity

Hi, room
Beginner of learning perl here!!

I have question to all,

I have below file name datebook.master which contains only 2 lines
Mike wolf:12/3/44:144 park ave, paramus: 44000
Sarah kim: 3/2/67:255 lomel ave, fort lee: 33000

Now, I am testing out below scripts and working fine.
-------------------- scripts 1------------------------------------------
#!/usr/bin/perl -w
open(FILE, "datebook.bak") || die "CAn't open $!\n";
while(<FILE>) {
@line=split(":");
print "$line[0] $line[2]\n";
}
close(FILE)
------------------------------------------------------------------------

And, I get desired results.
However, if I do a script w/ below, it does not work.

-------------------- scripts 2------------------------------------------
#!/usr/bin/perl -w
open(FILE, "datebook.bak") || die "CAn't open $!\n";
@lines=(<FILE>);
while(<@lines>) {
@line=split(":");
print "$line[0] $line[2]\n";
}
close(FILE)
------------------------------------------------------------------------

Can someone tell me why this is happening?
I want script 2 since I need to call this array again in the later parts of
the scripts(in the longer version).

Can someone kindly respond?

Big thanks in advance.
Jul 19 '05 #1
3 4352
rx****@hehe.com wrote:
However, if I do a script w/ below, it does not work.
<snip>
while(<@lines>) {
Replace that with

for (@lines) {
Can someone tell me why this is happening?


http://www.perldoc.com/perl5.8.0/pod/perlsyn.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #2
rx****@hehe.com wrote in message news:<8a**************************@posting.google. com>...
Hi, room
Beginner of learning perl here!!

I have question to all,

I have below file name datebook.master which contains only 2 lines
Mike wolf:12/3/44:144 park ave, paramus: 44000
Sarah kim: 3/2/67:255 lomel ave, fort lee: 33000

Now, I am testing out below scripts and working fine.
-------------------- scripts 1------------------------------------------
#!/usr/bin/perl -w
open(FILE, "datebook.bak") || die "CAn't open $!\n";
while(<FILE>) {
@line=split(":");
print "$line[0] $line[2]\n";
}
close(FILE)
------------------------------------------------------------------------

And, I get desired results.
However, if I do a script w/ below, it does not work.

-------------------- scripts 2------------------------------------------
#!/usr/bin/perl -w
open(FILE, "datebook.bak") || die "CAn't open $!\n";
@lines=(<FILE>);
while(<@lines>) {
@line=split(":");
print "$line[0] $line[2]\n";
}
close(FILE)
------------------------------------------------------------------------

Can someone tell me why this is happening?
I want script 2 since I need to call this array again in the later parts of
the scripts(in the longer version).

Can someone kindly respond?

Big thanks in advance.


Change while(<@lines>) to foreach (@lines)

++imanshu.
Jul 19 '05 #3
hi******@gdit.iiit.net (Himanshu Garg) wrote in message news:<a4**************************@posting.google. com>...
rx****@hehe.com wrote in message news:<8a**************************@posting.google. com>...
Hi, room
Beginner of learning perl here!!

I have question to all,

I have below file name datebook.master which contains only 2 lines
Mike wolf:12/3/44:144 park ave, paramus: 44000
Sarah kim: 3/2/67:255 lomel ave, fort lee: 33000

Now, I am testing out below scripts and working fine.
-------------------- scripts 1------------------------------------------
#!/usr/bin/perl -w
open(FILE, "datebook.bak") || die "CAn't open $!\n";
while(<FILE>) {
@line=split(":");
print "$line[0] $line[2]\n";
}
close(FILE)
------------------------------------------------------------------------

And, I get desired results.
However, if I do a script w/ below, it does not work.

-------------------- scripts 2------------------------------------------
#!/usr/bin/perl -w
open(FILE, "datebook.bak") || die "CAn't open $!\n";
@lines=(<FILE>);
while(<@lines>) {
@line=split(":");
print "$line[0] $line[2]\n";
}
close(FILE)
------------------------------------------------------------------------

Can someone tell me why this is happening?
I want script 2 since I need to call this array again in the later parts of
the scripts(in the longer version).

Can someone kindly respond?

Big thanks in advance.


Change while(<@lines>) to foreach (@lines)

++imanshu.

thank you all, I have used for and it's working like charms..
I was doing excersise on the book that I recently purchased so that I
can practice on perl by myself and it was getting tough to do this
particular one cause I couldn't understand what was going on. Below is
entire script(please do advise as I am real beginner and if I made
really short script long one.)

also, this --> @all_lines = map { split(":") } @lines; , not
understanding
why split command would not work w/out map.......

At all rate, thank you very much.
----------------------------------------------------------------
#!/usr/bin/perl

open (FILEINFO, "file.txt") || die "Can't open file $!\n";
@lines=<FILEINFO>;
close FILEINFO;
@all_lines = map { split(":") } @lines;

print "Who would you like to find? \n";
chomp($looking_for=<STDIN>) ;
print "You are looking for $looking_for \n";
$count_look = grep(/$looking_for/i, @all_lines);
@count_look = grep(/$looking_for/i, @all_lines);

print " Number of person that matched was $count_look \n";
print " They are @count_look \n";
for (@lines) {
( $name, $phone, $address, $bd, $sal )=split(":");
print "$name\t $phone\n";
}

sleep(1);
print "Who are you searching for ?";
chomp($looking_for1=<STDIN>) ;
@looking_for2=grep(/$looking_for1/i, @lines);
$looking_for2=grep(/$looking_for1/i, @lines);
( $name_l, $phone_l, $address_l, $bd_l, $sal_l )=map {split(":") }
@looking_for2;
print "@looking_for2\n";
sleep(1);
print "What is the new phone number for $looking_for1 ?";
chomp($new_numb=<STDIN>);
print "$looking_for1\'s phone number is currently $phone_l\n";
@newinfo = split(":", @looking_for2);
@newinfo1 = splice(@newinfo, 1,1, "$new_numb");

print "Here is the line showing the new phone number:\n";
print join(":", $name_l, $new_numb, $address_l, $bd_l, $sal_l), "\n";
print "$looking_for1 was found in the array $looking_for2 times. \n";
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
4
by: qwweeeit | last post by:
The standard split() can use only one delimiter. To split a text file into words you need multiple delimiters like blank, punctuation, math signs (+-*/), parenteses and so on. I didn't...
9
by: Will McGugan | last post by:
Hi, I'm curious about the behaviour of the str.split() when applied to empty strings. "".split() returns an empty list, however.. "".split("*") returns a list containing one empty string. ...
2
by: Jay | last post by:
So I'm writting this software that talks to an IRC server; and occasionaly IRC servers send back data two lines at a time with lines breaks inside. For my own sanity I had been using the split...
5
by: jmdocherty | last post by:
All, I've been trying to set up a CSS layout and all seems well in Firefox but in Safari it just seems to be plain weird! I hope someone can help tell me whether this is a problem with my code...
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
6
by: Rex the Strange | last post by:
Hello all, Traditionally I'm a Delphi and VB6 programmer and have recently started working with VB.NET (which, I might add, I love, but that's beside the point). Anyway, I was trying to make a...
6
by: Peted | last post by:
Hi wondering what is the best way to do this Need a user to click a button, that sends 3 or 4 string based commands via a TCP/ip socket link I can connect to the ip device no problems, am...
6
by: Joel Koltner | last post by:
I normally use str.split() for simple splitting of command line arguments, but I would like to support, e.g., long file names which-- under windows -- are typically provided as simple quoted...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.